Home > Back-end >  How to replace string in pandas data frame to NaN?
How to replace string in pandas data frame to NaN?

Time:03-23

Let's consider data frame following:

enter image description here

I want to change string type elements of this DataFrame into NaN's. Example of an solution would be:

frame.replace("k", np.NaN)
frame.replace("s", np.NaN)

However it would be very problematic in bigger data sets to go through each element, checking if this element is string and changing it at the end. Do you know any simpler solution?

Desire table:

enter image description here

CodePudding user response:

Use pd.to_numeric to transform all non numeric values to nan:

frame = frame.apply(pd.to_numeric, errors='coerce')

CodePudding user response:

You can use astype(str) and .str.digit for each column to get a mask for values that are numbers, and then just index the dataframe with that mask to make NaN the values that aren't masked:

df = df[df.astype(str).apply(lambda col: col.str.isdigit())]

Output:

>>> df
   0    1    2
0  1    2  NaN
1  2  NaN    4
2  5  NaN    1

CodePudding user response:

Use df.replace regex

import numpy as np
df.replace(regex='[A-Za-z]', value=np.nan)
  • Related