简体   繁体   English

用列表中的项目填充Pandas数据框中的缺失值

[英]Fill missing values in Pandas dataframe with items in list

What is the best way to fill missing values in dataframe with items from list? 用列表中的项目填充数据框中缺失值的最佳方法是什么? For example: 例如:

pd.DataFrame([[1,2,3],[4,5],[7,8],[10,11,12],[13,14]])

        0   1   2
    0   1   2   3
    1   4   5 NaN
    2   7   8 NaN
    3  10  11  12
    4  13  14 NaN

list = [6, 9, 150]

to get some something like this: 得到这样的东西:

       0   1   2
   0   1   2   3
   1   4   5   6
   2   7   8   9
   3  10  11  12
   4  13  14  15

this is actually a little tricky and a bit of a hack, if you know the column you want to fill the NaN values for then you can construct a df for that column with the indices of the missing values and pass the df to fillna : 这实际上有点棘手,有点骇人听闻,如果您知道要填充NaN值的列,则可以为该列构造一个df,其中包含缺失值的索引,并将df传递给fillna

In [33]:
fill = pd.DataFrame(index =df.index[df.isnull().any(axis=1)], data= [6, 9, 150],columns=[2])
df.fillna(fill)

Out[33]:
    0   1    2
0   1   2    3
1   4   5    6
2   7   8    9
3  10  11   12
4  13  14  150

You can't pass a dict (my original answer) as the dict key values are the column values to match on and the scalar value will be used for all NaN values for that column which is not what you want: 您无法传递字典(我的原始答案),因为字典键值是要匹配的列值,并且标量值将用于该列的所有NaN值,这不是您想要的值:

In [40]:
l=[6, 9, 150]
df.fillna(dict(zip(df.index[df.isnull().any(axis=1)],l)))

Out[40]:
    0   1   2
0   1   2   3
1   4   5   9
2   7   8   9
3  10  11  12
4  13  14   9

You can see that it has replaced all NaN s with 9 as it matched the missing NaN index value of 2 with column 2 . 您会看到它用9替换了所有NaN ,因为它与第2列匹配缺少的NaN索引值2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM