sobrio35
sobrio35

Reputation: 123

How can I replace all instances of * in a dataframe?

I'd like to remove all instances of '*' from my dataframe, pitcher, specifically in the 'Name' column.

     Rk     Name                  Team    IP   R
887  888.0  Huascar Ynoa\ynoahu01  ATL   3.0   6
888  889.0  Alex Young*\youngal01  ARI  34.2  10   

I've tried the snippets below, as well as anything I could find only (regex=True and other variations). Also tried using a for loop on the strings individually, but each time I print either the new Series/Dataframe, the * are still there

pitcher = pitcher.replace('*', '')

pitcher['Name'] = pitcher['Name'].replace('*', '')

newdf = pd.DataFrame()
newdf['newname'] = pitcher['Name'].replace('*', '')

The simplest idea would be great. I'm sure there are many ways to do this.

Upvotes: 0

Views: 169

Answers (1)

Stef
Stef

Reputation: 30609

To replace '*' anywhere in the dataframe we'll first have to find all string columns and then replace the '*' there. Strings have the object type, so this will work in most trivial cases:

for c in pitcher.select_dtypes(['object']).columns:
    pitcher.loc[:,c] = pitcher.loc[:,c].str.replace('*','')

Please note that mixed type columns or other object type columns (dicts, lists etc.) are of object type too. In these cases we can use:

pitcher = pitcher.applymap(lambda x: x.replace('*','') if isinstance(x,str) else x)

Upvotes: 1

Related Questions