How to fill dataframe Nan values with empty list [] in pandas?

后端 未结 11 790
粉色の甜心
粉色の甜心 2020-12-24 11:02

This is my dataframe:

          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,         


        
11条回答
  •  渐次进展
    2020-12-24 11:45

    Surprisingly, passing a dict with empty lists as values seems to work for Series.fillna, but not DataFrame.fillna - so if you want to work on a single column you can use this:

    >>> df
         A    B    C
    0  0.0  2.0  NaN
    1  NaN  NaN  5.0
    2  NaN  7.0  NaN
    >>> df['C'].fillna({i: [] for i in df.index})
    0    []
    1     5
    2    []
    Name: C, dtype: object
    

    The solution can be extended to DataFrames by applying it to every column.

    >>> df.apply(lambda s: s.fillna({i: [] for i in df.index}))
        A   B   C
    0   0   2  []
    1  []  []   5
    2  []   7  []
    

    Note: for large Series/DataFrames with few missing values, this might create an unreasonable amount of throwaway empty lists.

    Tested with pandas 1.0.5.

提交回复
热议问题