Not able to rename columns

Screen Link: http://127.0.0.1:8888/notebooks/exit%20survey.ipynb

My Code:

name = {'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'}


tafe_survey_updated.rename(columns=name, inplace=True)
tafe_survey_updated.columns

What I expected to happen: All columns to be renamed

What actually happened:

Index(['id', 'Institute', 'WorkArea', 'cease_date', 'separationtype',
       'Contributing Factors. Career Move - Public Sector ',
       'Contributing Factors. Career Move - Private Sector ',
       'Contributing Factors. Career Move - Self-employment',
       'Contributing Factors. Ill Health',
       'Contributing Factors. Maternity/Family',
       'Contributing Factors. Dissatisfaction',
       'Contributing Factors. Job Dissatisfaction',
       'Contributing Factors. Interpersonal Conflict',
       'Contributing Factors. Study', 'Contributing Factors. Travel',
       'Contributing Factors. Other', 'Contributing Factors. NONE',
       'Gender.     What is your Gender?', 'CurrentAge.     Current Age',
       'Employment Type.     Employment Type',
       'Classification.     Classification', 'institute_service',
       'role_service'],
      dtype='object')

some columns names like 'Gender. What is your Gender?','CurrentAge. Current Age','Classification. Classification' and 'Employment Type. Employment Type'
were not renamed to gender, age, position,and employment_status respectively. please guys help.Just for clarification I am working the project locally.

1 Like

Hi @brayanopiyo18,

I’ve just tried running your code in my project (also locally). At the beginning the columns were not renamed all, because of the issues with the number of spaces in the names of some columns, but after fixing them everything were renamed correctly. So just be carefull with extra spaces in the column names.

Here is my fixed code:

name = {'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'}


tafe_survey_updated.rename(columns=name, inplace=True)
tafe_survey_updated.columns

and the output:

Index(['id', 'Institute', 'WorkArea', 'cease_date', 'separationtype',
       'Contributing Factors. Career Move - Public Sector ',
       'Contributing Factors. Career Move - Private Sector ',
       'Contributing Factors. Career Move - Self-employment',
       'Contributing Factors. Ill Health',
       'Contributing Factors. Maternity/Family',
       'Contributing Factors. Dissatisfaction',
       'Contributing Factors. Job Dissatisfaction',
       'Contributing Factors. Interpersonal Conflict',
       'Contributing Factors. Study', 'Contributing Factors. Travel',
       'Contributing Factors. Other', 'Contributing Factors. NONE', 'gender',
       'age', 'employment_status', 'position', 'institute_service',
       'role_service'],
      dtype='object')
1 Like

Thanks a lot @Elena_Kosourova for your concern. Unfortunately that didn’t work as well even after fixing the spaces.
here is my fixed code

name = {'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'}


tafe_survey_updated.rename(columns=name, inplace=True)
tafe_survey_updated.columns

the output

Index(['id', 'Institute', 'WorkArea', 'cease_date', 'separationtype',
       'Contributing Factors. Career Move - Public Sector ',
       'Contributing Factors. Career Move - Private Sector ',
       'Contributing Factors. Career Move - Self-employment',
       'Contributing Factors. Ill Health',
       'Contributing Factors. Maternity/Family',
       'Contributing Factors. Dissatisfaction',
       'Contributing Factors. Job Dissatisfaction',
       'Contributing Factors. Interpersonal Conflict',
       'Contributing Factors. Study', 'Contributing Factors. Travel',
       'Contributing Factors. Other', 'Contributing Factors. NONE',
       'Gender.     What is your Gender?', 'CurrentAge.     Current Age',
       'Employment Type.     Employment Type',
       'Classification.     Classification', 'institute_service',
       'role_service'],
      dtype='object')

Hi @brayanopiyo18

I just tried the name you used, the one shown in the output and the one Elena used. From the looks of it they all look good. Though visually from your code and the output, looks like some spaces are still missing. Anyway it didn’t show here when I tried to reproduce it here.

'Gender. What is your Gender?'
'Gender. What is your Gender?'
'Gender. What is your Gender?

'Classification. Classification'
'Classification. Classification'
'Classification. Classification'

But are you copying the names of these column names from df.column command?
If not, please run this command, and copy and paste the column names to rename from here and try again. Let’s hope that will sort out.

2 Likes

@ brayanopiyo18 Brayanopiyo
Why don’t you try:

tafe_survey_updated.columns = [ ‘id’, ‘cease_date’, ‘separationtype’, ’ gender, ‘age’, ‘employment_status’, ‘position’, ‘institute_service’, ‘role_service’]

I’m not sure if it can help you, but it’s another column renaming process.

2 Likes

Uhm… It looks strange :thinking:
Probably not the most elegant approach, but you can try this then:

name = {'Record ID': 'id',
'CESSATION YEAR': 'cease_date',
'Reason for ceasing employment': 'separationtype',
'Gender. What is your Gender?': 'gender',
tafe_survey_updated.columns[-6]: 'age',
tafe_survey_updated.columns[-5]: 'employment_status',
tafe_survey_updated.columns[-4]: 'position',
tafe_survey_updated.columns[-3]: 'institute_service',
'LengthofServiceCurrent. Length of Service at current workplace (in years)': 'role_service'}


tafe_survey_updated.rename(columns=name, inplace=True)
tafe_survey_updated.columns
1 Like

No, in this way the number of columns will mismatch. Or otherwise, it’s necessary to insert in this list also the names of the columns that will not be renamed.

1 Like

hi @Elena_Kosourova, @jithins123 , @raisa.jerin.sristy79,

I want to thank you all for taking your time going through the problem. Actually this the strange code ever interacted with in this journey of data science , where I had to spend over ten hours trying to get the breakthrough.

Luckily through interactions with you guys I came to know where the problem was.it seems like I was using the original CSV files which is slightly different with the one of Dataquest guided project due to modifications made by the Dataquest.

So I had to update my CSV file in the excel, where I minimised the white spaces of these columns 'Gender. What is your Gender?','CurrentAge. Current Age','Employment Type. Employment Type,'Classification. Classification after which everything worked out well.

Also, I assumed it could work better with regex, which I am not so good at and have no otherwise but to move to the dataquest community instead :grinning:

Otherwise guys, thanks for all your contribution once more and wishing you Happy learning!

2 Likes

@brayanopiyo18 that’s great that you’ve managed to resolve it! Oh, this issue with the original csv file was not obvious at all, well done to find and fix it! :star_struck:

1 Like

Try this:

tafe_survey_updated = tafe_survey_updated.rename(columns = {‘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’})

1 Like