3

Given this data frame:

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 entire data frame with asterisks. I've tried this:

d.replace('99','*')

...but it only worked in the case of the string 99 in column B.

Thanks in advance!

0

4 Answers 4

4

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 that inplace instead:

d.replace('99','*',regex=True,inplace=True)
Sign up to request clarification or add additional context in comments.

Comments

3

If you want to replace all the 99s , try using regex

>>> d.astype(str).replace('99','*',regex=True)

    A   B   C
0   a   1   abcd*
1   b   2   4
2   *   *   5

Comments

2

Use numpys character functions

d.values[:] = np.core.defchararray.replace(d.values.astype(str), '99', '*')
d

   A  B      C
0  a  1  abcd*
1  b  2      4
2  *  *      5

naive time test

enter image description here

Comments

2

Problem is 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 cast your dataframe to string type via 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 as given by other answers. I for some reason missed the abcd* in your DataFrame.

Will leave this here, just in case it is helpful to someone else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.