Ric*_*cky 5 python dictionary pandas
我有一个数据帧和下面的字典,但我如何用字典替换列?
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'}
Run Code Online (Sandbox Code Playgroud)
我使用"for"句子进行替换,但它很慢,就像那样:
for i in data.index:
data.loc[i,'occupation_detailed'] = dict1[data.loc[i,'occupation_code']]
Run Code Online (Sandbox Code Playgroud)
由于我的数据包含100万行,如果我只运行1000次,则需要几秒钟.半百万行可能需要半天!
那么有没有更好的方法呢?
非常感谢您的建议!
使用map,如果缺少一些值,请NaN:
print (df)
occupation_code
index
0 10
1 16
2 12
3 7
4 1
5 3
6 10
7 7
8 1
9 3
10 4
11 100 <- add missing value 100
Run Code Online (Sandbox Code Playgroud)
df['occupation_code'] = df['occupation_code'].map(dict1)
print (df)
occupation_code
index
0 K-12student
1 self-employed
2 programmer
3 executive/managerial
4 academic/educator
5 clerical/admin
6 K-12student
7 executive/managerial
8 academic/educator
9 clerical/admin
10 college/grad student
11 NaN
Run Code Online (Sandbox Code Playgroud)
另一个解决方案是使用replace,如果缺少一些值得到原始值,则不NaN:
df['occupation_code'] = df['occupation_code'].replace(dict1)
print (df)
occupation_code
index
0 K-12student
1 self-employed
2 programmer
3 executive/managerial
4 academic/educator
5 clerical/admin
6 K-12student
7 executive/managerial
8 academic/educator
9 clerical/admin
10 college/grad student
11 100
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48 次 |
| 最近记录: |