Python Forum
dataframe groupby with totals for certain fields
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dataframe groupby with totals for certain fields
#2
Your for-loop iterates over two items (or a few number of columns)... I don't understand why it is expensive (probably, copying and concatenating huge df's are expensive).
Alternative way is to calculate cumulative values as a separate df and concatenate it to the source df, e.g.

gg=df.groupby('region').agg({'numbers':'mean', 'respondent': pd.Series.nunique}).reset_index()
gg['illness'] = 'All'
pd.concat([df, gg], ignore_index=True)
The same can be done with 'illness' and finally, without groupby (i.e. df.agg(...))
Reply


Messages In This Thread
RE: dataframe groupby with totals for certain fields - by scidam - Oct-19-2020, 12:21 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020