【发布时间】:2021-10-09 05:00:34
【问题描述】:
使用熊猫=1.1.5。我想将值从 df2 更新为 df1。但是 df2 有新的索引,当我使用更新时,这些索引并没有附加到 df1。详情见下文。谢谢
df1
| Revenue | Profit | Sales |
0 | 100 | 300 | 1 |
1 | 500 | 900 | 3 |
2 | 200 | 100 | 4 |
df2
| Sales |
0 | 10 |
6 | 12 |
所需的df
| Revenue | Profit | Sales |
0 | 100 | 300 | 10 |
1 | 500 | 900 | 3 |
2 | 200 | 100 | 4 |
6 | Nan | Nan | 12 |
df 使用更新
df1.update(df2)
| Revenue | Profit | Sales |
0 | 100 | 300 | 10 |
1 | 500 | 900 | 3 |
2 | 200 | 100 | 4 |
【问题讨论】:
-
这是
combine_first的经典用例:df2.combine_first(df1).loc[:,[*df1]]
标签: python python-3.x pandas