python - Replace NaN with empty list in a pandas dataframe -
i'm trying replace nan values in data empty list []. list represented str , doesn't allow me apply len() function. there anyway replace nan value actual empty list in pandas?
in [28]: d = pd.dataframe({'x' : [[1,2,3], [1,2], np.nan, np.nan], 'y' : [1,2,3,4]}) in [29]: d out[29]: x y 0 [1, 2, 3] 1 1 [1, 2] 2 2 nan 3 3 nan 4 in [32]: d.x.replace(np.nan, '[]', inplace=true) in [33]: d out[33]: x y 0 [1, 2, 3] 1 1 [1, 2] 2 2 [] 3 3 [] 4 in [34]: d.x.apply(len) out[34]: 0 3 1 2 2 2 3 2 name: x, dtype: int64
this works using isnull
, loc
mask series:
in [90]: d.loc[d.isnull()] = d.loc[d.isnull()].apply(lambda x: []) d out[90]: 0 [1, 2, 3] 1 [1, 2] 2 [] 3 [] dtype: object in [91]: d.apply(len) out[91]: 0 3 1 2 2 0 3 0 dtype: int64
you have using apply
in order list object not interpreted array assign df try align shape original series
edit
using updated sample following works:
in [100]: d.loc[d['x'].isnull(),['x']] = d.loc[d['x'].isnull(),'x'].apply(lambda x: []) d out[100]: x y 0 [1, 2, 3] 1 1 [1, 2] 2 2 [] 3 3 [] 4 in [102]: d['x'].apply(len) out[102]: 0 3 1 2 2 0 3 0 name: x, dtype: int64
Comments
Post a Comment