Snorrlaxxx
Snorrlaxxx

Reputation: 168

Aggregate pandas dataframe by a column

Suppose I have a dataframe that looks like this

enter image description here

I want to aggregate this so that I just have this

enter image description here

How would I do this with pandas?

Upvotes: 0

Views: 52

Answers (1)

MrNobody33
MrNobody33

Reputation: 6483

Try with pd.DataFrame.groupby, and then agg with last and sum

df.groupby('Tract').agg({'Year':'last','a':'sum','b':'sum','c':'sum'})

Output:

       Year     a   b   c
Tract               
1111    2016    5   5   5

Upvotes: 3

Related Questions