[英]Pandas DataFrame) one column replace other df
我有两个 pandas DataFrame
# python 3
one is | A | B | C | and another is | D | E | F |
|---|---|---| |---|---|---|
| 1 | 2 | 3 | | 3 | 4 | 6 |
| 4 | 5 | 6 | | 8 | 7 | 9 |
| ......... | | ......... |
我想得到“预期”的结果
expected result :
| A | D | E | F | C |
|---|---|---|---|---|
| 1 | 3 | 4 | 6 | 3 |
| 4 | 8 | 7 | 9 | 6 |
| ................. |
df1['B'] convert into df2
我努力了
pd.concat([df1,df2], axis=1, sort=False)
and drop column df['B']
但它似乎效率不高。 可以通过使用 insert() 或其他方法解决吗?
我认为您的方法很好,您也可以在concat
之前删除列:
pd.concat([df1.drop('B', axis=1),df2], axis=1, sort=False)
DataFrame.join
的另一种方法:
df1.drop('B', axis=1).join(df2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.