0

I have been playing with aggregation in pandas dataframe. Considering the following dataframe:

df=pd.DataFrame({'a':[1,2,3,4,5,6,7,8],
                 'batch':['q','q','q','w','w','w','w','e'],
                 'c':[4,1,3,4,5,1,3,2]})

I have to do aggregation on the batch column with mean for column a and min for column c. I used the following method to do the aggregation:

agg_dict = {'a':{'a':'mean'},'c':{'c':'min'}}
aggregated_df = df.groupby("batch").agg(agg_dict)

The problem is that I want the final data frame to have the same columns as the original data frame with the slight difference of having the aggregated values present in each of the columns. The result of the above aggregation is a multi-index data frame, and am not sure how to convert it to an individual data frame? I followed the link: Reverting from multiindex to single index dataframe in pandas . But, this didn't work, and the final output was still a multi-index data frame. Great, if someone could help

phoenix97
  • 179
  • 3
  • 11
  • Check `df.groupby("batch", as_index=False).agg({'a': 'mean','c': 'min'})` – Shubham Sharma Sep 05 '20 at 08:15
  • This worked. So, just a followup, mkaing the individual columns as a list, essentially increases the index of the final dataframe? – phoenix97 Sep 05 '20 at 08:17
  • Not sure what you mean, can you please elaborate? – Shubham Sharma Sep 05 '20 at 08:31
  • What I mean is in case, I have the groupby operation as: `df.groupby("batch",as_index=False).agg({'a':{'a':{'a':'mean'},{'a':'std'}},'c':'min'})`. This would ideally mean that the levels of index is higher? – phoenix97 Sep 05 '20 at 08:52
  • Please check [`this post`](https://stackoverflow.com/questions/12589481/multiple-aggregations-of-the-same-column-using-pandas-groupby-agg) – Shubham Sharma Sep 05 '20 at 09:00

1 Answers1

0

you can try the following code df.groupby('batch').aggregate({'c':'min','a':mean})