Skip to content
Advertisement

How to sum a value based on group?

I am trying to figure out how to sum a value from rank 5 to the LOWEST rank (I.E. 5-1,000) for each geography in my dataframe.

df = df.set_index('geography')

df["Rank"] = df.groupby("geography")["value_to_sum"].rank("dense", ascending=True)

df = df.groupby('geography').iloc('Rank')[5, :]['value_to_sum'].sum(axis=1)

However, I am getting the error: ‘DataFrameGroupBy’ object has no attribute ‘iloc’

Am I using iloc incorrectly?

Advertisement

Answer

IIUC, try:

>>> df[df.groupby("geography")["value_to_sum"].rank("dense").lt(5)].sum(axis=1)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement