2017-01-04 66 views
1

我的目標合併兩個數據幀,同時保持一定的排

我想合併的同時保持連續兩個數據幀,row_to_keep

數據幀

>>> df 

       ColumnA    Stats 
0    Cake    872 
1    Cheese Cake   912 
2    Egg     62 
3    Raspberry Jam  091 
4    Bacon    123 
5    Bread    425 
row_to_keep  NaN     999 

>>> df1 

       ColumnB 
0    Cake 
1    Cheese Cake  
3    Raspberry Jam 
4    Bacon 

我嘗試

new_df = df.merge(df1, left_on="ColumnA", right_on="ColumnB") 

>>> new_df 

       ColumnA   Stats ColumnB 
0    Cake   872  Cake 
1    Cheese Cake  912  Cheese Cake 
3    Raspberry Jam 091  Raspberry Jam 
4    Bacon   123  Bacon 

期望輸出

合併按預期工作,但我在努力尋找一種有效的方式來保持的df最後一排。

   ColumnA   Stats 
0    Cake   872 
1    Cheese Cake  912 
3    Raspberry Jam 091 
4    Bacon   123 
row_to_keep  NaN    999 

此外,會有這樣的方法,該方法得到該輸出,通過使用'row_to_keep'代替row[number]

+1

...怎麼樣做左連接的'通過'merge'方法將df'轉換爲'df1'? –

+0

或者您可以稍後將該行添加到'new_df'。 –

回答

1

UPDATE:

In [139]: df[df.ColumnA.isin(df1.ColumnB)].append(df.loc['row_to_keep']) 
Out[139]: 
        ColumnA Stats 
0      Cake 872 
1    Cheese Cake 912 
3   Raspberry Jam  91 
4     Bacon 123 
row_to_keep   NaN 999 

老答案:

這裏是一個解決方案:

In [126]: df.merge(df1, left_on="ColumnA", right_on="ColumnB").append(df.loc['row_to_keep']) 
Out[126]: 
        ColumnA Stats  ColumnB 
0      Cake 872   Cake 
1    Cheese Cake 912 Cheese Cake 
2   Raspberry Jam  91 Raspberry Jam 
3     Bacon 123   Bacon 
row_to_keep   NaN 999   NaN 

說明:

df.loc['row_to_keep']塞萊CTS一行通過指數值('row_to_keep')和DF.append(row) - 追加到合併後的DF

我必須承認,雖然,有可能不太難看的解決方案......

+0

對我來說這夠好!謝謝:)我可以通過參考我的問題快速請求解釋/澄清'.loc'嗎? – LearningToPython

+1

@ BenF97,我爲答案添加了一個非常簡短的解釋。您可能還想閱讀[關於索引的不同選擇](http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing) – MaxU

+0

甜蜜;-)非常感謝你! – LearningToPython