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

后端 未结 11 766
粉色の甜心
粉色の甜心 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:28

    You can first use loc to locate all rows that have a nan in the ids column, and then loop through these rows using at to set their values to an empty list:

    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]
    

提交回复
热议问题