首页 > 解决方案 > 从数据框列中删除类似空格+字母+空格的字符串

问题描述

我在数据框中有这些句子列:

"I love x cat"
"You x x"
"x x x x"
"This example is better"

我想用 python 删除“x”

"I love cat"
"You"
""
"This example is better"

但我不知道我怎么能得到它,因为单词 example 有“x”,我不想删除它

谢谢!

标签: pythonpython-3.xpandasdataframe

解决方案


如果你有一个数据框,那么你可以使用:

df['your col name here'] = df['your col name here'].apply(lambda s: ' '.join(i for i in s.split(' ') if i != 'x'))

推荐阅读