python - Pandas - 用相应的 id 列值填充缺失的列值

标签 python json pandas missing-data

我正在寻找用 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   

最佳答案

如果空值为 NoneNaN,我认为您需要:

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)

关于python - Pandas - 用相应的 id 列值填充缺失的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48479162/

相关文章:

javascript - 如何将json数组设置为html表单div。?

python-2.7 - 基于行索引、列引用的数据框中的返回值

python - 如何检测哪些快捷键映射到 Vim 中的特定命令?

python - 如何将 .txt 文件中的每个单词添加到 Python 列表中?

python - 对数图中的重叠轴刻度标签

json - Cloudformation 找不到区域 Opsworks 堆栈

javascript - Ruby on Rails 和 JS - 如何在 Javascript 中获取 Json 对象?

Python:需要帮助拆分二进制代码的输入,没有空格

python-3.x - pandas:按列分组后如何获得第一个正数?

python - 如何计算 Pandas 滚动窗口的累积乘积?