大熊猫的求和功能

时间:2020-03-26 02:02:50

标签: python pandas

我正在尝试对变量“ Hill-Burton Funds”求和,并按“ stateyear”分组:

ds['Hill-Burton Funds'].replace(",", "")
ds['Hill-Burton Funds'].astype(str).astype(int)
ds['hbfunds'] = ds.groupby(['stateyear'])['Hill-Burton Funds'].sum()

尽管首先将“希尔伯顿基金”从一个对象转换为整数,但是当我使用sum()时,它返回的变量(“ hbfunds”)只是将数字汇总在一起而不是将它们相加(例如2000和2001年从20001年改为20002001年)。请任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

使用:

ds['Hill-Burton Funds'] = ds['Hill-Burton Funds'].replace(",", "").astype(str).astype(int)
ds['hbfunds'] = ds.groupby(['stateyear'])['Hill-Burton Funds'].sum()