slovakia111
slovakia111

Reputation: 1

Sum function with pandas

I'm trying to sum the variable "Hill-Burton Funds", grouping by "stateyear":

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

Despite first converting "Hill-Burton Funds" from an object to an integer, when I use sum() it is returning the variable ("hbfunds") that simply aggregates the numbers together instead of adding them(ex. 2000 and 2001 are becoming 20002001 instead of 4001). Please any help is appreciated!

Upvotes: 0

Views: 35

Answers (1)

DarioHett
DarioHett

Reputation: 152

Use:

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

Upvotes: 1

Related Questions