0
col1 col2 col3
jake  male   180
jake  male    140
john   female 230
john   male    100

let say we have this kind of df
if I want to group col1, and col2 and make new dataframe like this

col1 col2 col3
jake  male  [180,140]<-mean
john  female [230]<-mean
john  male   [100]<-mean

I tryied to use the group by

keyword_mean = df.groupby(['col1','col2'])['col3'].agg(['mean']).reset_index()

but it keep making the duplicated col1 and col2

alift
  • 1,855
  • 2
  • 13
  • 28

1 Answers1

2

If you're looking for a pd.Series of the means without any group info, try:

df.groupby(["col1", "col2"]).mean().reset_index()["col3"]
ngwalton
  • 383
  • 3
  • 8