Home > Net >  How to remove/replace any string from a dataframe?
How to remove/replace any string from a dataframe?

Time:02-05

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.

CodePudding user response:

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)
  • Related