我的目标
我想合并两个数据框,同时保留行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]
来获取此输出?
答案 0 :(得分:1)
<强>更新强>
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']
按索引值('row_to_keep'
)和DF.append(row)
选择一行 - 将其附加到合并后的DF
我必须承认,可能会有不那么难看的解决方案......