我希望用JSON文件中的相应代码键填充缺少的列值,基于下面的代码会抛出TypeError: 'list' object is not callable
。我的阅读和填写缺失值的代码如下。
data = json.load((open('world_bank_projects.json')))
themecodes = json_normalize(data, 'mjtheme_namecode')
d = themecodes.sort_values('name', na_position='last').set_index('code')['name'].to_dict()
themecodes.loc[themecodes['name'].isnull(), 'name'] = themecodes['code'].map(d)
themecodes.head(20)
code name
0 8 Human development
1 11
2 1 Economic management
3 6 Social protection and risk management
4 5 Trade and integration
5 2 Public sector governance
6 11 Environment and natural resources management
7 6 Social protection and risk management
8 7 Social dev/gender/inclusion
9 7 Social dev/gender/inclusion
10 5 Trade and integration
11 4 Financial and private sector development
12 6 Social protection and risk management
13 6
14 2 Public sector governance
15 4 Financial and private sector development
16 11 Environment and natural resources management
17 8
18 10 Rural development
19 7
答案 0 :(得分:4)
如果空值为None
s或NaN
s:
d = themecodes.sort_values('name', na_position='first').set_index('code')['name'].to_dict()
themecodes.loc[themecodes['name'].isnull(), 'name'] = themecodes['code'].map(d)
或者:
themecodes['name'] = themecodes['name'].combine_first(themecodes['code'].map(d))
themecodes['name'] = (themecodes.sort_values('name', na_position='last')
.groupby('code')['name']
.transform(lambda x: x.fillna(x.iat[0]))
.sort_index())
print (themecodes)
code name
0 8 Human development
1 11 Environment and natural resources management
2 1 Economic management
3 6 Social protection and risk management
4 5 Trade and integration
5 2 Public sector governance
6 11 Environment and natural resources management
7 6 Social protection and risk management
8 7 Social dev/gender/inclusion
9 7 Social dev/gender/inclusion
10 5 Trade and integration
11 4 Financial and private sector development
12 6 Social protection and risk management
13 6 Social protection and risk management
14 2 Public sector governance
15 4 Financial and private sector development
16 11 Environment and natural resources management
17 8 Human development
18 10 Rural development
19 7 Social dev/gender/inclusion
解决方案,如果需要替换空白空间或一些空格:
d = themecodes.sort_values('name', na_position='first').set_index('code')['name'].to_dict()
themecodes.loc[themecodes['name'].str.strip() == '', 'name'] = themecodes['code'].map(d)