这是我的数据框:
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, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,... 2 2011-04-25 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,... 3 2011-04-26 Nan 4 2011-04-27 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,... 5 2011-04-28 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...
我想替换Nan为[]。怎么做?Fillna([])无效。我什至尝试过replace(np.nan, [])但它给出了错误:
Nan
replace(np.nan, [])
TypeError('Invalid "to_replace" type: \'float\'',)
您可以先使用loc来查找nan该ids列中所有具有的行,然后使用at将它们的值设置为空列表来遍历这些行:
loc
nan
ids
at
for row in df.loc[df.ids.isnull(), 'ids'].index: df.at[row, 'ids'] = [] >>> df 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, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] 2 2011-04-25 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] 3 2011-04-26 [] 4 2011-04-27 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] 5 2011-04-28 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]