Multiple Logical Comparisons In Pandas Df
Solution 1:
You can use numpy.select
to structure your multiple conditions. The final parameter represents default value.
conditions = [(df.A > df.B) & (df.B > df.C),
(df.A < df.B) & (df.B < df.C)]
values = [1, 2]
df['E'] = np.select(conditions, values, 3)
There are several alternatives: nested numpy.where
, sequential pd.DataFrame.loc
, pd.DataFrame.apply
. The main benefit of this solution is readability while remaining vectorised.
Solution 2:
you can use apply
on df with your two conditions such as:
df['E'] = df.apply(lambda x: 1 if x.A > x.B and x.B > x.C else 2 if x.A < x.B and x.B < x.C else 3, axis=1)
Solution 3:
This can also be solved using indexing and fillna.
df.loc[(df['A'] > df['B'])
&(df['B'] > df['C']), 'New_Col'] = 1
df.loc[(df['A'] < df['B'])
&(df['B'] < df['C']), 'New_Col'] = 2
df['New_Col'] = df['New_Col'].fillna(3)
The first chunk of code is read like so: locate where A > B and B > C, if both of these conditions are true, set the column 'New_Col' equal to 1. The second chunk can be interpreted in the same way. If both the first and second chunk do no return a 1 or 2, then they will appear as null. Use the fillna() function to fill those nulls with a 3.
This will produce the following dataframe:
Post a Comment for "Multiple Logical Comparisons In Pandas Df"