#๐Ÿ”’ AttributeError when trying to rename column header on Pandas dataframe

26 messages ยท Page 1 of 1 (latest)

deft panther
#

Trying to rename the column header on a dataframe before I write it to an excel sheet. The dataframe only has one column and I get this error when writing. The df will write just fine if I don't try to rename it.

AttributeError: 'NoneType' object has no attribute 'to_excel'

df2 = df2.rename(columns={'service_groups': 'Service Groups'}, inplace=True)
df2.to_excel(writer, sheet_name="Summary", startrow=0, startcol=start_column, index=False)```
verbal stormBOT
#

@deft panther

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

cinder canopy
deft panther
#

yes. Inplace

#

I changed that after I copied my code to the clipboard

cinder canopy
#

inplace means change the current object, do not return anything so df2 was replaced with None

proper scaffold
#

It's one of weird things pandas does, you can optimise the stuff by doing stuff in place, but then the method returns None instead of the result (so it behaves like any in-place thing usually behaves)

cinder canopy
#

you could either remove df2 = or inplace = True and it should work

deft panther
#

I removed inplace=True and df2 = and it doesn't rename. Still service_groups.

#

new code

df2.rename(columns={"service_groups": "Service Groups"})
df2.to_excel(writer, sheet_name="Summary", startrow=0, startcol=start_column, index=False)```
broken pine
#

Either, not both. ```py
df2 = df2.rename(columns={'service_groups': 'Service Groups'})

or

df2.rename(columns={'service_groups': 'Service Groups'}, inplace=True)```

deft panther
#

I did it both ways as @broken pine mentioned and neither work to change the header.

cinder canopy
deft panther
#

Index(['service_group'], dtype='object')

cinder canopy
#

service_group not service_groups, you are trying to rename nonexisting column

deft panther
#

๐Ÿคฆโ€โ™‚๏ธ

#

I'm a moron.

raven pewter
#

If you use inplace=True, the DataFrame is changed in place, meaning the method returns nothing, but the object is modified. In that case only, do not assign its output (you'll get df2 == None)

If you do not use inplace arg, the method will return a changed DataFrame object, and that must be assigned in order to "save" that change.

#

Using inplace is generally discouraged due to this confusion. Most of the time, just use assignment, or chain methods together.

cinder canopy
#

simple mistake to make, best to just copy-paste the actual column names that you can print out, lets you be sure you're not making a typo

deft panther
#

Thanks all.

#

!close

#

!close

verbal stormBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.