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

Python 如果单元格内容少于指定的字符数,则删除单元格内容,python,pandas,replace,Python,Pandas,Replace,我有以下数据帧: >>>name location problems 0 Lena Haifa ,, 1 Layla Aman not enough points 2 Dili Istanbul , ... 如果少于5个字母,我想更改单元格内容, 所以我会得到这张桌子: >>>name location problems 0 Lena Haifa 1 Layla A

我有以下数据帧:

>>>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   
...
(请记住,)

我怎样才能做到这一点?

试着这样做:

df.loc[df['problems'].str.len()<5,'problems'] = ''
df.loc[df['problems'].str.len()您可以使用以下方法:

或者如果您想要一个空字符串而不是
NaN

df['col'] = df['col'].where(df['col'].str.len() >= 5, other='')

这里有另一种使用
np的方法。其中

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['problems']=np.where(df['problems'].str.len()
打印(df)
名称位置问题
0丽娜海法
1 Layla Aman分数不够
2帝力伊斯坦布尔
使用分配功能:

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