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

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

    My approach is similar to @hellpanderrr's, but instead tests for list-ness rather than using isnan:

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

    I originally tried using pd.isnull (or pd.notnull) but, when given a list, that returns the null-ness of each element.

提交回复
热议问题