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

3
  • 2
    you're probably working with a Series, rather than a DataFrame after .agg - what happens if you display it or call type()? Commented Mar 15, 2022 at 18:24
  • 1
    Please provide a minimal reproducible example. How to make good reproducible pandas examples Commented Mar 15, 2022 at 18:37
  • 1
    Thank you @ti7 that worked didn't realize they were series and not dataframes :) Commented Mar 15, 2022 at 20:43

2 Answers 2

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'})
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.