如何在pandas中用空列表[]填充dataframe Nan值?

2024-04-25 14:48:45 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的数据帧:

          date                          ids
0     2011-04-23  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...
1     2011-04-24  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...
2     2011-04-25  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...
3     2011-04-26  Nan
4     2011-04-27  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...
5     2011-04-28  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...

我想用[]替换Nan。怎么做?Fillna([])不起作用。我甚至试过replace(np.nan, []),但它给出了错误:

 TypeError('Invalid "to_replace" type: \'float\'',)

Tags: to数据idsdatetype错误npnan
3条回答

我的方法类似于@hellpanderr,但不是使用isnan,而是测试列表的完整性:

df['ids'] = df['ids'].apply(lambda d: d if isinstance(d, list) else [])

我最初尝试使用pd.isnull(或pd.notnull),但是,当给定一个列表时,它返回每个元素的空值。

经过大量的挠头,我发现这个方法应该是最有效的(没有循环,没有应用),只需分配给一个片段:

isnull = df.ids.isnull()

df.loc[isnull, 'ids'] = [ [[]] * isnull.sum() ]

诀窍是构造一个大小合适的[]isnull.sum())列表,然后将其括在一个列表中:您要分配的值是一个包含空列表作为元素的2D数组(1列,isnull.sum()行)。

您可以首先使用loc来定位在ids列中具有nan的所有行,然后使用at循环这些行以将其值设置为空列表:

for row in df.loc[df.ids.isnull(), 'ids'].index:
    df.at[row, 'ids'] = []

>>> df
        date                                             ids
0 2011-04-23  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
1 2011-04-24  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
2 2011-04-25  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
3 2011-04-26                                              []
4 2011-04-27  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
5 2011-04-28  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

相关问题 更多 >