0

I am working on a Pandas dataframe in which users' occupations are defined with numbers.

This is how my data looks like

according to the metadata, the numbers are chosen from the following list:

*  0:  "other" or not specified
*  1:  "academic/educator"
*  2:  "artist"
*  3:  "clerical/admin"
*  4:  "college/grad student"
*  5:  "customer service"
*  6:  "doctor/health care"
*  7:  "executive/managerial"
*  8:  "farmer"
*  9:  "homemaker"
* 10:  "K-12 student"
* 11:  "lawyer"
* 12:  "programmer"
* 13:  "retired"
* 14:  "sales/marketing"
* 15:  "scientist"
* 16:  "self-employed"
* 17:  "technician/engineer"
* 18:  "tradesman/craftsman"
* 19:  "unemployed"
* 20:  "writer"

I need to replace these numbers with the right occupation titles. I have already created the following dictionary, But I don't know what is the best way to replace the values.
I appreciate your help :)

occupation={0:"other or not specified", 1:"academic/educator", 2:"artist", 3:"clerical/admin", 4:"college/grad student", 5:"customer service", 6:"doctor/health care", 7:"executive/managerial" , 8:"farmer", 9:"homemaker",10:"K-12 student", 11:"lawyer", 12:"programmer", 13:"retired", 14:"sales/marketing", 15:"scientist", 16:"self-employed", 17:"technician/engineer",18:"tradesman/craftsman", 19:"unemployed",20:"writer"}

vtasca
  • 1,660
  • 11
  • 17
SHOGANAI
  • 1
  • 4

1 Answers1

1

This can be achieved using pandas.Series.map, as follows:

df['occupation'].map(occupation)
vtasca
  • 1,660
  • 11
  • 17