seq*_*ard 3 python merge dataframe 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
Run Code Online (Sandbox Code Playgroud)
我现在想要做的是将 Column1 与 ColumnA 进行比较,并将 df2 的行附加到在 Column1 中与 df2 在 A 列中具有相同值的 df1 行,以便结果如下所示:
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
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用 pandas .groupby() 函数并将列 1 和 A 设置为键,比较它们然后合并键相同的分组对象,但我找不到一种有效的方法来比较分组对象的键2 个数据帧。有人知道如何做到这一点吗?
您可以merge为 lhs 和 rhs 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3948 次 |
| 最近记录: |