gpt4 book ai didi

Python Pandas : summing value of two or more DataFrames with identical value in multiple columns

转载 作者:行者123 更新时间:2023-12-04 07:40:43 25 4
gpt4 key购买 nike

我有两个数据帧,例如:

df1 = pd.DataFrame([["tom", 1, 2, 3], ["bob", 3, 4, 5], ["ali", 6, 7, 8]], columns=["name", "A", "B", "C"])
df1
Out[44]:
name A B C
0 tom 1 2 3
1 bob 3 4 5
2 ali 6 7 8
df2 = pd.DataFrame([["rob", 1, 2, 3], ["ali", 6, 7, 8]], columns=["name", "A", "B", "D"])
df2
Out[46]:
name A B D
0 rob 1 2 3
1 ali 6 7 8
如何对具有相同“名称”和相同列的值执行求和运算,并获得如下结果 DataFrame:
  name A   B   C   D
0 tom 1 2 3 NaN # <- tom and bob don't shows up in df2, so the sum is identical
1 bob 3 4 5 NaN # to their values in df1
2 rob 1 2 NaN 3 # <- rob only shows up on df2, so the sum equal to its df2 values
3 ali 12 14 8 8 # <- ali's A and B are sum up, and C and D are identical to their
# corresponding value in df1 and df2
请注意,我不知道两个 DataFrame 的“名称”列中会显示哪些名称。
而且,因为我有两个以上的这样的 DataFrame 需要总结,如果可能的话,我怎样才能在一次操作中对所有这些数据帧进行总结,而不是一个一个地总结呢?非常感谢。

最佳答案

希望这能解决您的问题。我已将 Nan 修改为 0。

import pandas as pd
df1 = pd.DataFrame([["tom", 1, 2, 3], ["bob", 3, 4, 5], ["ali", 6, 7, 8]], columns=["name", "A", "B", "C"])
df2 = pd.DataFrame([["rob", 1, 2, 3], ["ali", 6, 7, 8]], columns=["name", "A", "B", "D"])
df3=pd.concat([df1, df2], ignore_index=True, sort=False)
df4=df3.groupby(['name'])['A','B','C','D'].sum()
print(df4)

关于Python Pandas : summing value of two or more DataFrames with identical value in multiple columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67481271/

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