0

Pandas has isnull() and fillna() methods to replace NaN values in DataFrames. I have a dataset that has mostly string typed columns, but some columns have a few floating point values scattered in them. Are there some equivalent methods in Pandas for finding and replacing these?

So if I have a DataFrame like this:

In [60]: df1=pd.DataFrame([[1.0,'foo'],[2.0,1.0],[float('NaN'),'bar'],[4.0,0.0],[5.0,'baz']],columns=['fval','sval'])
In [61]: df1
Out[61]: 
   fval sval
0   1.0  foo
1   2.0    1
2   NaN  bar
3   4.0    0
4   5.0  baz

In [63]: df1.isnull()
Out[63]: 
    fval   sval
0  False  False
1  False  False
2   True  False
3  False  False
4  False  False

...I can replace the NaN values in the 'fval' column like this:

In [64]: df1.fillna(2.5)
Out[64]: 
   fval sval
0   1.0  foo
1   2.0    1
2   2.5  bar
3   4.0    0
4   5.0  baz

Is there convenient method in Pandas to replace the 0 and 1 values in the 'sval' column with, say, 'na'? How about an equivalent to is isnull() for out-of-place values?

2
  • Please share sample data and expected output. Commented Mar 29, 2020 at 12:34
  • same methods should work..? if the NaN are string you can replace them first with np.nan using df.replace Commented Mar 29, 2020 at 12:35

2 Answers 2

1

If you want to manullay replace strings you can use the following replace statement:

df1.replace([0, 1], "na")

All values that are 0 or 1 will be replaced with the string "na".

However, as @anky_91 pointed out, you can also replace your specified values with np.nan. After your replacement, you can identify your NaN values just like the once in the float typed columns. This probably what you are actually looking for.

df1.replace([0, 1], np.nan)

More Information on how to use replace you can find here.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the array arg on replace(). But that doesn't really generalize well in real situations, I have a more diverse set of values.
0

Guess there's no Pandas-native way of doing this. But using apply gets what I want:

df1['sval'].apply(lambda val: str(val) if type(val)!=str else val)

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.