I have a case of needing to replace string values across a number of columns:
- The value to substitute changes per column
- I need to preserve existing NaN
I have a series of steps that seems that it should work, to me, but does not; the 'inplace' step does not work. Some dummy test code:
make a dataframe
df = pd.DataFrame([[np.nan, 2, np.nan, np.nan],
[3, 4, np.nan, 1],
[np.nan, np.nan, np.nan, 5],
[np.nan, 3, np.nan, 'foo']],
columns=list('ABCD'))
calculate substitute value, say from last column
special_value = pd.to_numeric(df['D'], errors='corece').min() / 2
special_value
0.5
have a look
df
seems to work here
pd.to_numeric(df['D'].dropna(), errors='coerce').fillna(value=special_value)
1 1.0
2 5.0
3 0.5
Name: D, dtype: float64
but no, it doesn't
pd.to_numeric(df['D'].dropna(), errors='coerce').fillna(value=special_value, inplace = True)
0 NaN
1 1
2 5
3 foo
Name: D, dtype: object
inplace=Truestrongly discouraged, anyway? IIRC they're likely going to get rid of it.