Guided Project: Exploring Ebay Car Sales Data <I am facing below error, need guidance>

My Code:

column_c = autos.columns 
column_c.rename({'yearOfRegistration':'registration_year'},axis=1,inplace = True)
column_c.rename({'monthOfRegistration':'registration_month'},axis=1,inplace = True)
column_c.rename({'notRepairedDamage':'unrepaired_damage'},axis=1,inplace = True)
column_c.rename({'dateCreated':'ad_created'},axis=1,inplace = True)
autos.columns = column_c

What I expected to happen:
Above code supposed to rename column names, but it is throwing below error.

What actually happened:

TypeErrorTraceback (most recent call last)
<ipython-input-29-a16b87f4f367> in <module>()
      1 column_c = autos.columns
----> 2 column_c.rename({'yearOfRegistration':'registration_year'},axis=1,inplace = True)
      3 column_c.rename({'monthOfRegistration':'registration_month'},axis=1,inplace = True)
      4 column_c.rename({'notRepairedDamage':'unrepaired_damage'},axis=1,inplace = True)
      5 column_c.rename({'dateCreated':'ad_created'},axis=1,inplace = True)

TypeError: rename() got an unexpected keyword argument 'axis'

I tried this after updating my pandas package, but still facing the issue. Can anyone please help me with this?
pandas version = 1.1.3

2 Likes

You should use autos dataframe instead of column_c.
Try this

autos.rename({'yearOfRegistration':'registration_year'},axis=1,inplace = True)
autos.rename({'monthOfRegistration':'registration_month'},axis=1,inplace = True)
autos.rename({'notRepairedDamage':'unrepaired_damage'},axis=1,inplace = True)
autos.rename({'dateCreated':'ad_created'},axis=1,inplace = True)

When you call column_c.rename... it will call the pandas.Index.rename() function of index type this as type of column_c is pandas.Index and that does not contain axis parameter that is why you got TypeError: rename() got an unexpected keyword argument 'axis' exception.

Here we supposed to use pandas.DataFrame.rename() function so when we use autos.rename... it will call pandas.DataFrame.rename() function of type DataFrame this as type of autos is DataFrame.

1 Like

Thank you!!! It worked.

1 Like