我在 pandas 中有两个数据框,如下所示:
df1: df2:
Column1 Column2 Column3 ColumnA ColumnB ColumnC
0 a x x 0 c y y
1 c x x 1 e z z
2 e x x 2 a s s
3 d x x 3 d f f
我现在要做的是将 Column1 与 Column 进行比较,并将 df2 的行附加到 df1 的 Column1 中与 df2 在 A 列中具有相同值的行,以便结果如下所示:
df1:
Column1 Column2 Column3 ColumnB ColumnC
0 a x x s s
1 c x x y y
2 e x x z z
3 d x x f f
我正在考虑使用 pandas .groupby() 函数并将列 1 和 A 设置为键,比较它们,然后合并键相同的分组对象,但我找不到一种有效的方法来比较分组对象的键2 个数据帧。有人知道如何做到这一点吗?
您可以指定要显示哪些列merge http://pandas.pydata.org/pandas-docs/stable/merging.html#database-style-dataframe-joining-merging对于左侧和右侧 dfs:
In [159]:
df1.merge(df2, left_on='Column1', right_on='ColumnA')
Out[159]:
Column1 Column2 Column3 ColumnA ColumnB ColumnC
0 a x x a s s
1 c x x c y y
2 e x x e z z
3 d x x d f f
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)