如果单元格内容少于指定的字符数,则删除单元格内容

2024-06-17 15:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧:

>>>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   
...

(请记住,)

我该怎么做


Tags: 数据name内容字母notlocationpointsproblems
3条回答

使用分配功能:

df = df.assgin(problem = lambda x: np.where(x['problem'].str.len()< 5,'',x['problem']))

下面是使用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  

             

试着这样做:

df.loc[df['problems'].str.len()<5,'problems'] = ''

相关问题 更多 >