262

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?

7 Answers 7

509

Use the vectorised str method replace:

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

df
      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 docs we see this description:

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

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

df = pd.DataFrame({'range':['(2,30)',',']})
df['range'].replace(',','-', inplace=True)

df['range']

0    (2,30)
1         -
Name: range, dtype: object

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

Sign up to request clarification or add additional context in comments.

2 Comments

I wonder why the warning occurs: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: pandas.pydata.org/pandas-docs/stable/user_guide/…
It's because of changes to Copy-On-Write semantics in pandas 2, which will be the only copy mechanic in pandas 3 pandas.pydata.org/docs/user_guide/…
134

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

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

1 Comment

me too, in my example: price_df['Mid-price (p)'].replace(',','',regex=True,inplace=True) worked. Withoutregex=True, it didn't.
29

If you only need to replace characters in one specific column, somehow regex=True and in place=True all failed, I think this way will work:

data["column_name"] = data["column_name"].apply(lambda x: x.replace("characters_need_to_replace", "new_characters"))

lambda is more like a function that works like a for loop in this scenario. x here represents every one of the entries in the current column.

The only thing you need to do is to change the "column_name", "characters_need_to_replace" and "new_characters".

2 Comments

This is what I ended up doing to replace, but I get the "A value is trying to be set on a copy of a slice from a DataFrame." warning. The resulting dataframe is exactly what I want, but the warning is off-putting. Rather than just turning the warning off, I'm in search of a non-warning-inducing way.
Thanks! Also you need to do some conversion in parameter of lambda function, like all x to string using str(x)
10

Replace all commas with underscore in the column names

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

Comments

8

In addition, for those looking to replace more than one character in a column, you can do it using regular expressions:

import re
chars_to_remove = ['.', '-', '(', ')', '']
regular_expression = '[' + re.escape (''. join (chars_to_remove)) + ']'

df['string_col'].str.replace(regular_expression, '', regex=True)

Comments

5

Almost similar to the answer by Nancy K, this works for me:

data["column_name"] = data["column_name"].apply(lambda x: x.str.replace("characters_need_to_replace", "new_characters"))

Comments

2

If you want to remove two or more elements from a string, example the characters '$' and ',' :

Column_Name
===========
$100,000
$1,100,000

... then use:

data.Column_Name.str.replace("[$,]", "", regex=True)

=> [ 100000, 1100000 ]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.