Hossein Amini
Hossein Amini

Reputation: 35

How to remove/replace any string from a dataframe?

I have a data frame including 950 rows and 204 columns, and I want to find and replace any possible string from this dataframe. When it is only one column I can simply do that through 2 lines code below:

 for i in df['name of column']:
     df[i].replace(r'^([A-Za-z]|[0-9]|_)+$', np.NaN, regex=True,inplace=True)

but now when it is more than 200 columns, how can I do that? Every help is appreciated.

Upvotes: 2

Views: 88

Answers (1)

Guru Stron
Guru Stron

Reputation: 142833

When it is only one column I can simply do that through 2 lines code below: ...

But you better should not - iterating dataframe can affect performance negatively (though currently you don't have that much data).

Just use replace on dataframe itself:

df = df.replace(r'^([A-Za-z]|[0-9]|_)+$', np.NaN, regex=True)

Upvotes: 3

Related Questions