Python (数据帧)一列替换另一列
我有两只熊猫数据帧Python (数据帧)一列替换另一列,python,pandas,dataframe,replace,concatenation,Python,Pandas,Dataframe,Replace,Concatenation,我有两只熊猫数据帧 # 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 | | ......... |
# 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)
另一种方法是:
为什么你认为它没有效率?我将尝试它作为你的好建议的第一个方法!非常感谢你,我希望你能圆满结束这一年
df1.drop('B', axis=1).join(df2)