python - 如何从列中删除不需要的字符串?
问题描述
我的列 df["reviews"] 中有评论,但只有一些评论以字符串“此信息有用吗?....”
结尾,所以如果我的行包含此字符串,我想删除最后的 42字符 [:-42] 其中包括 此信息是否有帮助?
我怎么能在 Pandas 中做到这一点
试过这个但它不起作用
def remove_unwanted(a):
if "Was this information helpful" in a:
print(a[:-42])
else:
print("False")
# column without yes and no in complaint body
df['cleaned_reviews'] = df.apply(lambda row: remove_unwanted(row['reviews']), axis = 1)
解决方案
试试这个,我想这就是你要找的
df['cleaned_reviews'] = df['reviews'].str.rstrip('Was this information helpful?')
推荐阅读
- ajax - 如何将 Ajax 调用发送到位于 Kubernetes 集群中 Pod 中的应用程序?
- cmd - cmd 复制除具有特定扩展名的文件之外的所有内容
- oracle - Oracle 不等式比较 - 无效数字
- php - Laravel 5.6:多个主键定义错误
- python - Pygame 多子弹问题
- python - 使用 LSTM 预训练 Word2Vec,预测句子中的下一个单词
- vue.js - 获取参数的方法在 Ajax 请求中发送
- haskell - 为什么它没有显示为已评估?
- javascript - 如何组合来自不同承诺的响应并将其作为单个 json 对象返回给客户端
- javascript - 如何在某些元素具有设定宽度后平均间隔 div