pandas 替换熊猫数据框中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34702338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:29:12  来源:igfitidea点击:

replace string in pandas dataframe

pythonpandasreplacedataframe

提问by DJK

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?

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

回答by Bill Harper

A dataframe in pandas is composed of columns which are series - Panda docs link

Pandas 中的数据框由系列的列组成 - Panda docs link

I'm going to use regex, because it's useful and everyone needs practice, myself included! Panda docs for text manipulation

我将使用正则表达式,因为它很有用,每个人都需要练习,包括我自己!用于文本操作的 Panda 文档

Note the str.replace. The regexstring you want is this (it worked for me): '.*@+.*' which says "any character (.) zero or more times (*), followed by an @ 1 or more times (+) followed by any character (.) zero or more times (*)

注意 str.replace。您想要的正则表达式字符串是这样的(它对我有用):'.*@+.*' 表示“任何字符 (.) 零次或多次 (*),然后是 @ 1 次或多次 (+)通过任何字符 (.) 零次或多次 (*)

df['column'] = df['column'].str.replace('.*@+.*', 'replacement')

Should work, where 'replacement' is whatever string you want to put in.

应该可行,其中“替换”是您要放入的任何字符串。

回答by ranlot

Assuming you called your dataframe df, you can do:

假设您调用了 dataframe df,您可以执行以下操作:

pd.DataFrame(map(lambda col: map(lambda x: 'anotherString' if '@' in x else x, df[col]), df.columns)).transpose()

回答by Ezer K

My suggestion:

我的建议:

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

not sure which is faster.

不确定哪个更快。