我有以下熊猫数据框架:
>>>name location problems
0 Lena Haifa ,,
1 Layla Aman not enough points
2 Dili Istanbul ,
...如果单元格的字母少于5个,我希望更改单元格的内容,因此我将获得以下表格:
>>>name location problems
0 Lena Haifa
1 Layla Aman not enough points
2 Dili Istanbul
...(删除,)
我该怎么做呢?
发布于 2020-11-02 01:11:10
下面是使用np.where的另一种方法
df['problems'] = np.where(df['problems'].str.len() < 5, '', df['problems'])
print(df)
name location problems
0 Lena Haifa
1 Layla Aman not enough points
2 Dili Istanbul 发布于 2020-11-02 01:01:50
像这样尝试:
df.loc[df['problems'].str.len()<5,'problems'] = ''发布于 2020-11-02 01:08:01
您可以使用where方法
df['col'] = df['col'].where(df['col'].str.len() >= 5)或者如果您想要一个空字符串而不是NaN
df['col'] = df['col'].where(df['col'].str.len() >= 5, other='')https://stackoverflow.com/questions/64634506
复制相似问题