2017-09-14 70 views
1

我有一個大數據框df,14列* 800行。獨立地,我有2列的文件(讓這些條目說):在熊貓數據框中,如何用元素y替換所有元素x?

car   one 
bus   two 
bike   three 
...   

我要搜索數據框df並替換出現在與同一行中的相應元素左欄的所有值右列 - 意味着,無論我在哪裏找到「汽車」作爲數據框中的條目,我都會將其替換爲「一」,無論我在哪裏找到「總線」,我都將其替換爲「二」等。我發現熊貓的dunctions.replace和.isin,但我缺乏將它們結合在一起的技巧。

任何人都可以告訴我如何在dataframe中進行替換嗎?


建議解決方案:

在列讀取(汽車,公共汽車,自行車,...)和(一,二,三,......)到numpy的陣列,列表,例如(汽車,公共汽車,自行車......)變成list_old和(一,二,三......)變成list_new。例如,這對於pandas pd.read_fwf是可行的。然後,人們可以使用熊貓的便利功能。替換功能:

df.replace(to_replace=list_old, value=list_new, inplace=True) 

而這個技巧!請注意,它也負責按索引匹配(list_old [n]替換爲list_new [n])

回答

1

假設您將文件加載到數據框df1中。用它首先生成一個映射:

mapping = dict(df1[['col1', 'col2'].values) 

或者,

mapping = df1.set_index('col1')['col2'] 

現在叫df.replace您的實際數據框,說df2

df2.replace(mapping) 

或者,你可以使用df.map - 非 - 匹配轉換爲NaN;

df2.map(mapping) 
+0

或'映射= df1.set_index('col1')['col2']' – Zero

2

我想你可以通過DataFrame.replaceSeries創建set_index

df = df.replace(df.set_index('col1')['col2']) 

或者通過dict

df = df.replace(df.set_index('col1')['col2'].to_dict()) 

樣品:

df = pd.DataFrame({'col1':['car','bus','bike'], 
        'col2':['one','two','three'], 
        'col3':['car','bike','bike']}) 
print (df) 
    col1 col2 col3 
0 car one car 
1 bus two bike 
2 bike three bike 

df = df.replace(df.set_index('col1')['col2']) 
print (df) 
    col1 col2 col3 
0 one one one 
1 two two three 
2 three three three