Python 如何替换 Pandas 数据框列中的字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28986489/
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-08-19 03:58:28  来源:igfitidea点击:

How to replace a characters in a column of a Pandas dataframe?

pythonreplacepandasdataframe

提问by UserYmY

I have a column in my dataframe like this:

我的数据框中有一列,如下所示:

range
(2,30)
(50,290)
(400,1000)
... 

and I want to replace the ,comma with -dash. I'm currently using this method but nothing is changed.

我想,-破折号替换逗号。我目前正在使用这种方法,但没有任何改变。

org_info_exc['range'].replace(',', '-', inplace=True)

Can anybody help?

有人可以帮忙吗?

采纳答案by EdChum

Use the vectorised strmethod replace:

使用矢量化str方法replace

In [30]:

df['range'] = df['range'].str.replace(',','-')
df
Out[30]:
      range
0    (2-30)
1  (50-290)

EDIT

编辑

So if we look at what you tried and why it didn't work:

因此,如果我们查看您尝试过的内容以及为什么它不起作用:

df['range'].replace(',','-',inplace=True)

from the docswe see this desc:

文档中我们看到了这个描述:

str or regex: str: string exactly matching to_replace will be replaced with value

str 或 regex: str: 与 to_replace 完全匹配的字符串将被替换为值

So because the str values do not match, no replacement occurs, compare with the following:

所以因为str值不匹配,所以不会发生替换,比较如下:

In [43]:

df = pd.DataFrame({'range':['(2,30)',',']})
df['range'].replace(',','-', inplace=True)
df['range']
Out[43]:
0    (2,30)
1         -
Name: range, dtype: object

here we get an exact match on the second row and the replacement occurs.

在这里,我们在第二行获得了完全匹配,并进行了替换。

回答by kevcisme

For anyone else arriving here from Google search on how to do a string replacement on allcolumns (for example, if one has multiple columns like the OP's 'range' column): Pandas has a built in replacemethod available on a dataframe object.

对于从谷歌搜索到这里的其他人,如何在所有列上进行字符串替换(例如,如果一个人有多个列,如 OP 的“范围”列):Pandas 有一个replace可用于数据框对象的内置方法。

df.replace(',', '-', regex=True)

df.replace(',', '-', regex=True)

Source: Docs

资料来源:文档

回答by Rameez Ahmad

Replace all commas with underscore in the column names

将列名中的所有逗号替换为下划线

data.columns= data.columns.str.replace(' ','_',regex=True)