0

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
Shaido
  • 27,497
  • 23
  • 70
  • 73
Michael Rut
  • 1,027
  • 1
  • 11
  • 19
  • "...if neither of the columns are not null." Do you mean that both of the columns are null, or was this simply a grammatical error....? – Yehuda Aug 17 '20 at 02:37
  • 1
    try this, https://stackoverflow.com/a/25479955/4985099 – sushanth Aug 17 '20 at 02:38

1 Answers1

2

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
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30