Michael Rut
Michael Rut

Reputation: 1027

pandas - get minimum value between two columns and assign if two columns are not null

I am trying to figure out how to assign the minimum value between two columns if neither of the columns are not null. So given a dataframe with have the following data populated in a row:

col1    col2    col3
347     933     338
938     523     211

I'm attempting to assign a temp column to the minimum values between col2 and col3, but the following gives me an error:

df.loc[df['col2'].notnull() & df['col3'].notnull(), 'TEMP_rank'] = min(df.loc[['col2'], df['col3']]).min().min()

I also have issues with:

df.loc[df['col2'].notnull() & df['col3'].notnull(), 'TEMP_rank'] = min(df.loc[['col2'], df['col3']]).min(1)

I'd be looking for the following output (testing between columns 2 & 3):

col1    col2    col3    tempCol
347     933     338     338
938     123     211     123

Upvotes: 0

Views: 1454

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31166

If you only want to calc min() when neither are null / NaN this does it.

df = pd.read_csv(io.StringIO("""col1    col2    col3
347     933     338
500     NaN     200
938     523     211"""), sep="\s+")

df = df.assign(
    tempCol=lambda dfa: np.where(dfa["col2"].isna()|dfa["col3"].isna(), 
                                 np.nan, 
                                 dfa.loc[:,["col2","col3"]].min(axis=1))
)

output

   col1   col2  col3  tempCol
0   347  933.0   338    338.0
1   500    NaN   200      NaN
2   938  523.0   211    211.0

Upvotes: 2

Related Questions