如何将DataFrame中的空列表转换为NaN?

我得到以下数据框:

enter image description here

我合并了两个不同的列表。 Standstillreason的单元格中可能包含带有内容的列表,或者可能已经是“ NaN”。我的目标是将空列表(如上图所示)也转换为“ NaN”。 我是一个初学者,我知道.replace方法。但这似乎不起作用吗?

df.Standstillreason = df.Standstillreason.replace("", np.nan, inplace=True)

I tried to replace the "" with [], " ", "[]" and "[ ]". But all this code did was to replace the whole column with None, even the non-empty cells. What can I do, to transform only those cells with empty lists to NaN?

评论
失忆症
失忆症

Let us try bool

df.loc[~df.Standstillreason.astype(bool),'Standstillreason']=np.nan

详情

s=pd.Series([[],[1]])
s
0     []
1    [1]
dtype: object
s.astype(bool)
0    False
1     True
dtype: bool
点赞
评论