Pandas Partial Data String Frame Replace
Given this dataframe:
import pandas as pd
d=pd.DataFrame({'A':['a','b',99],'B':[1,2,'99'],'C':['abcd99',4,5]})
d
A B C
0 a 1 abcd*
1 b 2 4
2 99 99 5
I want to replace all 99s in the whole dataframe with asterisks. I've tried this:
d.replace('99','*')
... but it only worked for row 99 in column B.
Thanks in advance!
+3
source to share
4 answers
This will do the job:
import pandas as pd
d=pd.DataFrame({'A':['a','b',99],'B':[1,2,'99'],'C':['abcd99',4,5]})
d=d.astype(str)
d.replace('99','*',regex=True)
which gives
A B C
0 a 1 abcd*
1 b 2 4
2 * * 5
Note that this creates a new DataFrame. You can also do this instead:
d.replace('99','*',regex=True,inplace=True)
+3
source to share
The problem is the values 99
in column A and B are of different types:
>>> type(d.loc[2,"A"])
<class 'int'>
>>> type(d.loc[2,"B"])
<class 'str'>
You can pass your dataframe to a string type using df.astype () and then replace, resulting in:
>>> d.astype(str).replace("99","*")
A B C
0 a 1 abcd99
1 b 2 4
2 * * 5
Edit: Using regex is the correct solution given by other answers. I missed abcd * in your DataFrame for some reason.
Leave it here just in case it helps someone else.
+2
source to share