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
A B C D 1 3 2 WD 3 WD NaN 2
should give me a new column like
Min 1 2
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
:
df['Min'] = df.apply(pd.to_numeric, errors='coerce').min(axis=1) print (df) A B C D Min 0 1 3 2.0 WD 1.0 1 3 WD NaN 2 2.0