Home > database >  How to add more than one dataframe column in pandas groupby function?
How to add more than one dataframe column in pandas groupby function?

Time:10-29

I have written the following codes in three separate cells in my jupyter notebook and have been able to generate the output I want. However, having this information in one dataframe will make it much easier to read.

How can I combine these separate dataframes into one so that the member_casual column is the index with max_ride_length, avg_ride_length and most_active_day_of_week columns next to it in the same dataframe?

enter image description here

CodePudding user response:

In the doc https://pandas.pydata.org/docs/reference/api/pandas.core.groupby.DataFrameGroupBy.aggregate.html

agg accepts a list a function as in the example:

df.groupby('A').agg(['min', 'max'])

CodePudding user response:

Malo is correct. I will expand a little bit because you can also name the columns when they are aggregated:

df.groupby('member_casual').agg(max_ride_length=('ride_length','max'), avg_ride_length=('ride_length','mean'), most_active_day_of_the_week=('day_of_week',pd.Series.mode))
  • Related