ng510
ng510

Reputation: 51

Minimum value for each row in pandas dataframe

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

Upvotes: 0

Views: 696

Answers (2)

Noor Ahmed Natali
Noor Ahmed Natali

Reputation: 523

list=DataFrame.min(axis=1, skipna=[Nan,WD],numeric_only=true)

DataFrame['min']=list

Upvotes: 0

jezrael
jezrael

Reputation: 862511

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

Upvotes: 1

Related Questions