'How to change NaN values to other values depending on columny type
I am trying to change NaN values in specific types of columns of a pandas dataframe.
I would like to do this with an if statement where I can use the isna() function and substitute NaN with text for the object type column and NaN with 0 values for the float64 type column.
Solution 1:[1]
You can use select_dtypes to get the wanted type, and fillna+update to updated the NAs/NaNs:
df = pd.DataFrame({'col1': [1,np.nan,3],
'col2': ['a',np.nan,'c'],
'col3': pd.Series(['a', pd.NA, 'c'], dtype='string')})
df.update(df.select_dtypes('number').fillna(-1))
df.update(df.select_dtypes('object').fillna('empty'))
df.update(df.select_dtypes('string').fillna('empty2'))
NB. check the types first with df.dtypes, in recent pandas versions it is possible to have 'string' as type for strings instead of 'object'
output:
col1 col2 col3
0 1.0 a a
1 -1.0 empty empty2
2 3.0 c c
input:
col1 col2 col3
0 1.0 a a
1 NaN NaN <NA>
2 3.0 c c
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
