2017-01-04 43 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