在大 pandas 数据框中替换字符串 [英] replace string in pandas dataframe

查看:187
本文介绍了在大 pandas 数据框中替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个列的数据框。我想查看一个列,如果列中的任何字符串包含@,我想用另一个字符串替换它们。我该怎么做?

解决方案

我的建议:

  df ['col'] = ['new string'if'@'in x else x for x in df ['col']] 

不知道哪个更快。


I have a dataframe with multiple columns. I want to look at one column and if any of the strings in the column contain @, I want to replace them with another string. How would I go about doing this?

解决方案

My suggestion:

df['col'] = ['new string' if '@' in x else x for x in df['col']]

not sure which is faster.

这篇关于在大 pandas 数据框中替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆