Not able to replace the string containing $ in pandas column
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I have a dataframe
df = pd.DataFrame('a':[1,2,3], 'b':[5, '12$sell', '1$sell'])
I want to replace $sell from column b.
So I tried replace()
method like below
df['b'] = df['b'].str.replace("$sell","")
but it's doesn't replace the given string and it gives me same dataframe as original.
It's working when I use it with apply
df['b'] = df['b'].apply(lambda x: str(x).replace("$sell",""))
So I want to know why it is not working in previous case?
Note: I tried replacing only $ and shockingly it works.
python string pandas series
add a comment |Â
up vote
6
down vote
favorite
I have a dataframe
df = pd.DataFrame('a':[1,2,3], 'b':[5, '12$sell', '1$sell'])
I want to replace $sell from column b.
So I tried replace()
method like below
df['b'] = df['b'].str.replace("$sell","")
but it's doesn't replace the given string and it gives me same dataframe as original.
It's working when I use it with apply
df['b'] = df['b'].apply(lambda x: str(x).replace("$sell",""))
So I want to know why it is not working in previous case?
Note: I tried replacing only $ and shockingly it works.
python string pandas series
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have a dataframe
df = pd.DataFrame('a':[1,2,3], 'b':[5, '12$sell', '1$sell'])
I want to replace $sell from column b.
So I tried replace()
method like below
df['b'] = df['b'].str.replace("$sell","")
but it's doesn't replace the given string and it gives me same dataframe as original.
It's working when I use it with apply
df['b'] = df['b'].apply(lambda x: str(x).replace("$sell",""))
So I want to know why it is not working in previous case?
Note: I tried replacing only $ and shockingly it works.
python string pandas series
I have a dataframe
df = pd.DataFrame('a':[1,2,3], 'b':[5, '12$sell', '1$sell'])
I want to replace $sell from column b.
So I tried replace()
method like below
df['b'] = df['b'].str.replace("$sell","")
but it's doesn't replace the given string and it gives me same dataframe as original.
It's working when I use it with apply
df['b'] = df['b'].apply(lambda x: str(x).replace("$sell",""))
So I want to know why it is not working in previous case?
Note: I tried replacing only $ and shockingly it works.
python string pandas series
python string pandas series
edited 1 hour ago
jpp
67k173984
67k173984
asked 2 hours ago
Akshay Nevrekar
2,29251331
2,29251331
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
5
down vote
accepted
It is regex metacharacter (end of string), escape it or add parameter regex=False
:
df['b'] = df['b'].str.replace("$sell","")
print (df)
a b
0 1 NaN
1 2 12
2 3 1
df['b'] = df['b'].str.replace("$sell","", regex=False)
If want also value 5, what is numeric, use Series.replace
with regex=True for replace substrings - numeric values are not touched:
df['b'] = df['b'].replace("$sell","", regex=True)
print (df['b'].apply(type))
0 <class 'int'>
1 <class 'str'>
2 <class 'str'>
Name: b, dtype: object
Or cast to strings all data of column:
df['b'] = df['b'].astype(str).str.replace("$sell","", regex=False)
print (df['b'].apply(type))
0 <class 'str'>
1 <class 'str'>
2 <class 'str'>
Name: b, dtype: object
And for better performance if no missing values is possible use list comprehension:
df['b'] = [str(x).replace("$sell","") for x in df['b']]
print (df)
a b
0 1 5
1 2 12
2 3 1
add a comment |Â
up vote
4
down vote
df['b'] = df['b'].str.replace("$sell","", regex=False)
add a comment |Â
up vote
4
down vote
$
is a regex special character. By default, pd.Series.str.replace
uses regex=True
.
Instead, specify regex=False
:
df['b'] = df['b'].str.replace('$sell', '', regex=False)
add a comment |Â
up vote
3
down vote
str.replace assumes a regex is being used. so you need to use escape i.e.
df['b'] = df['b'].str.replace("$sell","")
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
It is regex metacharacter (end of string), escape it or add parameter regex=False
:
df['b'] = df['b'].str.replace("$sell","")
print (df)
a b
0 1 NaN
1 2 12
2 3 1
df['b'] = df['b'].str.replace("$sell","", regex=False)
If want also value 5, what is numeric, use Series.replace
with regex=True for replace substrings - numeric values are not touched:
df['b'] = df['b'].replace("$sell","", regex=True)
print (df['b'].apply(type))
0 <class 'int'>
1 <class 'str'>
2 <class 'str'>
Name: b, dtype: object
Or cast to strings all data of column:
df['b'] = df['b'].astype(str).str.replace("$sell","", regex=False)
print (df['b'].apply(type))
0 <class 'str'>
1 <class 'str'>
2 <class 'str'>
Name: b, dtype: object
And for better performance if no missing values is possible use list comprehension:
df['b'] = [str(x).replace("$sell","") for x in df['b']]
print (df)
a b
0 1 5
1 2 12
2 3 1
add a comment |Â
up vote
5
down vote
accepted
It is regex metacharacter (end of string), escape it or add parameter regex=False
:
df['b'] = df['b'].str.replace("$sell","")
print (df)
a b
0 1 NaN
1 2 12
2 3 1
df['b'] = df['b'].str.replace("$sell","", regex=False)
If want also value 5, what is numeric, use Series.replace
with regex=True for replace substrings - numeric values are not touched:
df['b'] = df['b'].replace("$sell","", regex=True)
print (df['b'].apply(type))
0 <class 'int'>
1 <class 'str'>
2 <class 'str'>
Name: b, dtype: object
Or cast to strings all data of column:
df['b'] = df['b'].astype(str).str.replace("$sell","", regex=False)
print (df['b'].apply(type))
0 <class 'str'>
1 <class 'str'>
2 <class 'str'>
Name: b, dtype: object
And for better performance if no missing values is possible use list comprehension:
df['b'] = [str(x).replace("$sell","") for x in df['b']]
print (df)
a b
0 1 5
1 2 12
2 3 1
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
It is regex metacharacter (end of string), escape it or add parameter regex=False
:
df['b'] = df['b'].str.replace("$sell","")
print (df)
a b
0 1 NaN
1 2 12
2 3 1
df['b'] = df['b'].str.replace("$sell","", regex=False)
If want also value 5, what is numeric, use Series.replace
with regex=True for replace substrings - numeric values are not touched:
df['b'] = df['b'].replace("$sell","", regex=True)
print (df['b'].apply(type))
0 <class 'int'>
1 <class 'str'>
2 <class 'str'>
Name: b, dtype: object
Or cast to strings all data of column:
df['b'] = df['b'].astype(str).str.replace("$sell","", regex=False)
print (df['b'].apply(type))
0 <class 'str'>
1 <class 'str'>
2 <class 'str'>
Name: b, dtype: object
And for better performance if no missing values is possible use list comprehension:
df['b'] = [str(x).replace("$sell","") for x in df['b']]
print (df)
a b
0 1 5
1 2 12
2 3 1
It is regex metacharacter (end of string), escape it or add parameter regex=False
:
df['b'] = df['b'].str.replace("$sell","")
print (df)
a b
0 1 NaN
1 2 12
2 3 1
df['b'] = df['b'].str.replace("$sell","", regex=False)
If want also value 5, what is numeric, use Series.replace
with regex=True for replace substrings - numeric values are not touched:
df['b'] = df['b'].replace("$sell","", regex=True)
print (df['b'].apply(type))
0 <class 'int'>
1 <class 'str'>
2 <class 'str'>
Name: b, dtype: object
Or cast to strings all data of column:
df['b'] = df['b'].astype(str).str.replace("$sell","", regex=False)
print (df['b'].apply(type))
0 <class 'str'>
1 <class 'str'>
2 <class 'str'>
Name: b, dtype: object
And for better performance if no missing values is possible use list comprehension:
df['b'] = [str(x).replace("$sell","") for x in df['b']]
print (df)
a b
0 1 5
1 2 12
2 3 1
edited 1 hour ago
answered 1 hour ago
jezrael
290k19205283
290k19205283
add a comment |Â
add a comment |Â
up vote
4
down vote
df['b'] = df['b'].str.replace("$sell","", regex=False)
add a comment |Â
up vote
4
down vote
df['b'] = df['b'].str.replace("$sell","", regex=False)
add a comment |Â
up vote
4
down vote
up vote
4
down vote
df['b'] = df['b'].str.replace("$sell","", regex=False)
df['b'] = df['b'].str.replace("$sell","", regex=False)
answered 1 hour ago
Charles R
437211
437211
add a comment |Â
add a comment |Â
up vote
4
down vote
$
is a regex special character. By default, pd.Series.str.replace
uses regex=True
.
Instead, specify regex=False
:
df['b'] = df['b'].str.replace('$sell', '', regex=False)
add a comment |Â
up vote
4
down vote
$
is a regex special character. By default, pd.Series.str.replace
uses regex=True
.
Instead, specify regex=False
:
df['b'] = df['b'].str.replace('$sell', '', regex=False)
add a comment |Â
up vote
4
down vote
up vote
4
down vote
$
is a regex special character. By default, pd.Series.str.replace
uses regex=True
.
Instead, specify regex=False
:
df['b'] = df['b'].str.replace('$sell', '', regex=False)
$
is a regex special character. By default, pd.Series.str.replace
uses regex=True
.
Instead, specify regex=False
:
df['b'] = df['b'].str.replace('$sell', '', regex=False)
answered 1 hour ago
jpp
67k173984
67k173984
add a comment |Â
add a comment |Â
up vote
3
down vote
str.replace assumes a regex is being used. so you need to use escape i.e.
df['b'] = df['b'].str.replace("$sell","")
add a comment |Â
up vote
3
down vote
str.replace assumes a regex is being used. so you need to use escape i.e.
df['b'] = df['b'].str.replace("$sell","")
add a comment |Â
up vote
3
down vote
up vote
3
down vote
str.replace assumes a regex is being used. so you need to use escape i.e.
df['b'] = df['b'].str.replace("$sell","")
str.replace assumes a regex is being used. so you need to use escape i.e.
df['b'] = df['b'].str.replace("$sell","")
answered 1 hour ago
sgDysregulation
1,95611125
1,95611125
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52554917%2fnot-able-to-replace-the-string-containing-in-pandas-column%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password