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

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

    After a lot of head-scratching I found this method that should be the most efficient (no looping, no apply), just assigning to a slice:

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

    The trick was to construct your list of [] of the right size (isnull.sum()), and then enclose it in a list: the value you are assigning is a 2D array (1 column, isnull.sum() rows) containing empty lists as elements.

提交回复
热议问题