AttributeError: 'DataFrame' object has no attribute 'rename'

PHorniak
New Contributor II

Hello, I am doing the Data Science and Machine Learning course.

The Boston housing has unintuitive column names. I want to rename them, e.g. so 'zn' becomes 'Zoning'.

When I run this command:

df_bostonLegible = df_boston.rename({'zn':'Zoning'}, axis='columns')

Then I get the error "AttributeError: 'DataFrame' object has no attribute 'rename'".

I did a websearch and found a few guides that were inapplicable:

https://stackoverflow.com/questions/28163439/attributeerror-dataframe-object-has-no-attribute-height...

https://stackoverflow.com/questions/38134643/data-frame-object-has-no-attribute

mathan_pillai
Databricks Employee
Databricks Employee

Hi @PHorniak

You can use

df_bostonLegible = df_boston.withColumnRenamed("zn", "Zoning")

please accept the answer if it works or revert back with questions

Thanks

View solution in original post

iosman
New Contributor II

wow great information totally love it buddy.

KrunalLathiya
New Contributor II

If df_boston is a DataFrame, but you still face issues, try an alternative syntax: df_boston = df_boston.rename(columns={'zn': 'Zoning'}).

Make sure df_boston is a proper DataFrame and you're using a recent version of Pandas.