1

im new to python, i used to code...

StoreGrouper= DonHenSaless.groupby('Store')
StoreGrouperT= StoreGrouper["SalesDollars"].agg(np.sum)
StoreGrouperT.rename(columns={SalesDollars:TotalSalesDollars})

to group stores and sum by the SalesDollars then rename SalesDollars to TotalSalesDollars. it outputted the following error...

NameError: name 'SalesDollars' is not defined

I also tried using quotes

StoreGrouper= DonHenSaless.groupby('Store')
StoreGrouperT= StoreGrouper["SalesDollars"].agg(np.sum)
StoreGrouperT= StoreGrouperT.rename(columns={'SalesDollars':'TotalSalesDollars'})

This output the error: rename() got an unexpected keyword argument 'columns'

Here is my df df

Danny Warner
  • 21
  • 1
  • 3
  • 2
    you're probably working with a Series, rather than a DataFrame after `.agg` - what happens if you display it or call `type()`? – ti7 Mar 15 '22 at 18:24
  • 1
    Please provide a [mre]. [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Mar 15 '22 at 18:37
  • 1
    Thank you @ti7 that worked didn't realize they were series and not dataframes :) – Danny Warner Mar 15 '22 at 20:43

2 Answers2

1

In order to rename a column you need quotes so it would be:

StoreGrouperT.rename(columns={'SalesDollars':'TotalSalesDollars'})

Also I usually assign it a variable

StoreGrouperT = StoreGrouperT.rename(columns={'SalesDollars':'TotalSalesDollars'})
Qoi VamBorg
  • 35
  • 1
  • 5
1

Use the pandas rename option to change the column name. You can also use inplace as true if you want your change to get reflected to the dataframe rather than saving it again on the df variable

df.rename(columns={'old_name':'new_name'}, inplace=True)