0

Let's consider following data frame:

enter image description here

I want to change string-type elements of this DataFrame into NaN. 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. Is there an easier solution?

Desired table:

enter image description here

3 Answers 3

3

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

frame = frame.apply(pd.to_numeric, errors='coerce')
Sign up to request clarification or add additional context in comments.

Comments

2

Use df.replace regex

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

Comments

1

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

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.