LKA
LKA

Reputation: 553

renaming columns after group by and sum in pandas dataframe

This is my group by command:

pdf_chart_data1 = pdf_chart_data.groupby('sell').value.agg(['sum']).rename(
    columns={'sum':'valuesum','sell' : 'selltime'}
)

I am able to change the column name for value but not for 'sell'. Please help to resolve this issue.

Upvotes: 18

Views: 85054

Answers (5)

mountrix
mountrix

Reputation: 1243

To avoid using the rename function, we can rename directly the output of the groupby in the apply function:

df.groupby("sell", as_index=False).apply(lambda g: pd.Series({"valuesum": g["value"].sum()}))

Upvotes: 1

Superpronker
Superpronker

Reputation: 329

The super fast way

df = df.groupby('col1')['col1'].count() # returns a series which has a name
df1= df.to_frame('new_name') # <- overwrites the name of the series

Upvotes: 0

If you join to groupby with the same index where one is nunique ->number of unique items and one is unique->list of unique items then you get two columns called Sport. Using as_index=False I was able to rename the second Sport name using rename then concat the two lists together and sort descending on sport and display the 10 five sportcounts.

 grouped=df.groupby('NOC', as_index=False)
 Nsport=grouped['Sport'].nunique()\
     .rename(columns={'Sport':'SportCount'})
 Nsport=Nsport.set_index('NOC')

 country_grouped=df.groupby('NOC')
 Nsport2=country_grouped['Sport'].unique()

 df2=pd.concat([Nsport,Nsport2],  join='inner',axis=1).reindex(Nsport.index)
df2=df2.sort_values(by=["SportCount"],ascending=False)


print(df2.columns)
for key,item in df2.head(5).iterrows():
   print(key,item)

Upvotes: 0

Joselin Ceron
Joselin Ceron

Reputation: 502

df = df.groupby('col1')['col1'].count()
df1= df.to_frame().rename(columns={'col1':'new_name'}).reset_index()

Upvotes: 4

jezrael
jezrael

Reputation: 863281

You cannot rename it, because it is index. You can add as_index=False for return DataFrame or add reset_index:

pdf_chart_data1=pdf_chart_data.groupby('sell', as_index=False)['value'].sum()
                              .rename(columns={'sum':'valuesum','sell' : 'selltime'})

Or:

pdf_chart_data1=pdf_chart_data.groupby('sell')['value'].sum()
                          .reset_index()
                          .rename(columns={'sum':'valuesum','sell' : 'selltime'})

Upvotes: 54

Related Questions