im trying to compute the minimum for each row in a pandas dataframe.
I would like to add a column that calculates the minimum values and ignores “NaN” and “WD”
For example
JavaScript
x
4
1
A B C D
2
1 3 2 WD
3
3 WD NaN 2
4
should give me a new column like
JavaScript
1
4
1
Min
2
1
3
2
4
I tried df.where(df > 0).min(axis=1)
and df.where(df != "NaN").min(axis=1)
without success
Advertisement
Answer
Convert values to numeric with non numeric to NaNs by errors='coerce'
in to_numeric
and DataFrame.apply
so is possible use min
:
JavaScript
1
6
1
df['Min'] = df.apply(pd.to_numeric, errors='coerce').min(axis=1)
2
print (df)
3
A B C D Min
4
0 1 3 2.0 WD 1.0
5
1 3 WD NaN 2 2.0
6