gpt4 book ai didi

python - Pandas :如何更新数据框并附加新条目?

转载 作者:行者123 更新时间:2023-12-01 23:13:01 26 4
gpt4 key购买 nike

<分区>

使用 Pandas =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 |

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com