Try to rename the columns, error shows up

I think my code is correct, but there is a error… why? Thank you in advance!!

Screen Link:

My Code:


What actually happened:

AttributeErrorTraceback (most recent call last)
<ipython-input-60-093b60d937c0> in <module>()
----> 1 tafe_survey_updated=tafe_survey_updated.rename(columns={'record_id':'id','reason_for_ceasing_employment':'separationtype','cessation_year':'cease_date','currentage':'age','gender._what_is_your_gender?':'gender'},inplace=True)

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

I don’t know if this is related to your error but this could be happening because you have assigned “True” to “inplace”. When you do that, you don’t need to reassign the results to another variable, it already updates it. So you can do either this:

tafe_survey_updated.rename(columns={* insert dictionary here *}, inplace=True)

or you can do this:

tafe_survey_updated = tafe_survey_updated.rename(columns={* insert dictionary here *})

Hi @candiceliu93

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

NoneType means that instead of an instance of whatever Class or Object you think you’re working with, you’ve actually got None .

In this case, When you use inplace as True then the rename function does not return anything i.e. None. It makes changes directly on the dataframe, so there is no need to assign it back.

1 Like

Thank you!!! your explanation is clear!!

1 Like

my answer to this task

mapping = {‘Record ID’: ‘id’,‘CESSATION YEAR’:‘cease_date’,‘Reason for ceasing employment’:‘separationtype’
,‘Gender. What is your Gender?’:‘gender’,‘CurrentAge. Current Age’:‘age’,
‘Employment Type. Employment Type’:‘employment_status’,
‘Classification. Classification’:‘position’,
‘LengthofServiceOverall. Overall Length of Service at Institute (in years)’:‘institute_service’,
‘LengthofServiceCurrent. Length of Service at current workplace (in years)’:‘role_service’}

and then you rename columns

fate_survey_updated = tafe_survey.rename(mapper=mapping, axis=1)