I have a dataframe and dict below, but how do I replace a column with a dict?
data index occupation_code 0 10 1 16 2 12 3 7 4 1 5 3 6 10 7 7 8 1 9 3 10 4 β¦β¦ dict1 = {0: 'other',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-12student',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 used the "for" clause to replace, but it is very slow, for example:
for i in data.index: data.loc[i,'occupation_detailed'] = dict1[data.loc[i,'occupation_code']]
Since my data contains 1 million rows, and it costs a few seconds if I run it only 1 thousand times. 1 million lines can cost half a day!
So, is there a better way to do this?
Thanks so much for the tips!