python - Pandas DataFrame)一列替换其他df
问题描述
我有两个熊猫数据框
# 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)
推荐阅读
- docker-compose - 无法将图像推送到 Azure Container Register。为什么创建的资源没有出现在 Azure 仪表板中?
- python-3.x - 如何使用来自 python 的基线连接 Hive 数据库?
- winforms - 系统 Windows 窗体 - 将项目列表绑定到列表视图控件
- lambda - 方案语言中的“@”是什么意思?
- reactjs - 有没有办法知道一个承诺是否没有得到解决?
- typescript - 如何扁平化具有嵌套子属性的对象类型?
- python - 我在使用 Netflix 数据时遇到了数据准备问题
- javascript - 如何使用 AWS-SDK 和 MulterS3 将文件上传到数字海洋空间
- saleor - Saleor 迁移失败(django.db.utils.DataError)
- vue.js - 以编程方式设置下拉选项