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

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

    Without assignments:

    1) Assuming we have only floats and integers in our dataframe

    import math
    df.apply(lambda x:x.apply(lambda x:[] if math.isnan(x) else x))
    

    2) For any dataframe

    import math
    def isnan(x):
        if isinstance(x, (int, long, float, complex)) and math.isnan(x):
            return True
    
    df.apply(lambda x:x.apply(lambda x:[] if isnan(x) else x))
    
    0 讨论(0)
  • 2020-12-24 11:45

    Maybe more dense:

    df['ids'] = [[] if type(x) != list else x for x in df['ids']]
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 11:51

    Create a function that checks your condition, if not, it returns an empty list/empty set etc.

    Then apply that function to the variable, but also assigning the new calculated variable to the old one or to a new variable if you wish.

    aa=pd.DataFrame({'d':[1,1,2,3,3,np.NaN],'r':[3,5,5,5,5,'e']})
    
    
    def check_condition(x):
        if x>0:
            return x
        else:
            return list()
    
    aa['d]=aa.d.apply(lambda x:check_condition(x))
    
    0 讨论(0)
  • 2020-12-24 11:52

    This is probably faster, one liner solution:

    df['ids'].fillna('DELETE').apply(lambda x : [] if x=='DELETE' else x)
    
    0 讨论(0)
提交回复
热议问题