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

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

提交回复
热议问题