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

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

    Maybe not the most short/optimized solution, but I think is pretty readable:

    # Packages
    import ast
    
    # Masking-in nans
    mask = df['ids'].isna()
    
    # Filling nans with a list-like string and literally-evaluating such string
    df.loc[mask, 'ids'] = df.loc[mask, 'ids'].fillna('[]').apply(ast.literal_eval)
    

    The drawback is that you need to load the ast package.

    EDIT

    I recently figured out the existence of the eval() built-in. This avoids importing any extra package.

    # Masking-in nans
    mask = df['ids'].isna()
    
    # Filling nans with a list-like string and literally-evaluating such string
    df.loc[mask, 'ids'] = df.loc[mask, 'ids'].fillna('[]').apply(eval)
    

提交回复
热议问题