zaphodb
zaphodb

Reputation: 505

Python Pandas: Replacing float values in String typed columns?

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?

Upvotes: 0

Views: 4860

Answers (2)

zaphodb
zaphodb

Reputation: 505

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)

Upvotes: 0

DataBach
DataBach

Reputation: 1645

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.

Upvotes: 1

Related Questions