我正在尝试对变量“ 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年)。请任何帮助表示赞赏!
答案 0 :(得分:1)
使用:
ds['Hill-Burton Funds'] = ds['Hill-Burton Funds'].replace(",", "").astype(str).astype(int)
ds['hbfunds'] = ds.groupby(['stateyear'])['Hill-Burton Funds'].sum()