Home > Software design >  Python | IF AND Statements
Python | IF AND Statements

Time:05-26

all:

I have a 'Current Result' in the form of a data frame in Python (depicting in Excel as an illustration).

I'd like to add a column that classifies whether a row is a 'PRIME' or an 'ALT' designation.

The rules for whether something is a 'ALT' is both of the following:

  • 'Group' column is not 'NaN'
  • AND
  • 'Utilization' is always 0

enter image description here

Is a nested IF / AND statement the only concept that will work or is there a more efficient way to do this? PS I am newer to Python.

CodePudding user response:

Its kind of hard to re-create without digestible data, but I believe this will get you what you need IIUC

condition_list = [df['Utilization']%2 == 0, (df['Group'] != np.nan) & ((df['Utilization'] == 0) | df['Utilization']%2 != 0)]
choice_list = ['Prime', 'ALT']
df['Prime/Alt'] = np.select(condition_list, choice_list, 0)
  • Related