如何将字典的值插入 pandas 中数据框的空值?

How to insert the values of dictionary into null values of a dataframe in pandas?

我是 pandas 的新手。我面临空值问题。我有一个包含 3 个值和键的字典,必须将其插入到一列缺失值中,我该怎么做?最后一个字key是列名的名字

In [57]: df
Out[57]: 
   a   b   c  d  
0  0   1   2  3  
1  0 NaN   0  1  
2  0 Nan   3  Nan 
3  0   1   2  5  
4  0 Nan   2  Nan 
In [58]: dict= {df_b : [11,22,44], df_d: [33,54]

我想要的输出如下。

Out[57]: 
   a   b   c  d  
0  0   1   2  3  
1  0   11  0  1  
2  0   22  3  33 
3  0   1   2  5  
4  0   44  2  54

根据您的数据

d = [[0,   1,   2,  3  ],
[0, np.nan,   0,  1  ],
[0, np.nan,   3,  np.nan], 
[0,   1,   2,  5  ],
[0, np.nan,   2,  np.nan]] ]
df = pd.DataFrame(d, columns=['a', 'b', 'c', 'd'])
d = {'df_b' : [11,22,44], 'df_d': [33,54]}

尝试pandas.isna()

for key in d:
    column_name = key.split('_')[-1]
    val = d[key]
    for i,v in zip(df[df[column_name].isna()].index, val):
        df.loc[i, column_name] = v

输出

a   b     c    d
0   1.0   2   3.0
0   11.0  0   1.0
0   22.0  3   33.0
0   1.0   2   5.0
0   44.0  2   54.0

您可以使用 df.locisnull() 到 select NaN 值,并将它们替换为列表中的项目。

import pandas as pd

import numpy as np

mydict = {'b' : [11,22,44], 'd': [33,54]}
df = pd.DataFrame({'a': [0,0,0,0,0], 'b': [1, np.nan, np.nan, 1, np.nan], 'c': [2,0,3,2,2], 'd': [3,1,np.nan,5,np.nan]})

for key in mydict:
    df.loc[df[key].isnull(), key] = mydict[key]

#   a     b  c     d
0  0   1.0  2   3.0
1  0  11.0  0   1.0
2  0  22.0  3  33.0
3  0   1.0  2   5.0
4  0  44.0  2  54.0