Pandas create a column based on the score of another column with multiple criteria

I would like to create a boolean column based on the evaluation of another column using pandas. Ideally, I would like to do this with a syntax similar to what I copied, but if this is not possible, I am open to other suggestions.

df is the pandas framework. AggRow is a data column with integers.

So, I have data for AggRow that have a range of values. I can successfully create a new conditionmet column based on one criterion, for example, if I want the condition to be true wherever AggRow is less than or equal to 6001:

conditionmet = df['AggRow'] <= 6001

But if I want the condition to be true, if AggRow is <= 6001 or between 10001 and 10009 inclusive, I am having problems. The following expression gives the conditionmet = True only for the first condition, that is, where AggRow <= 6001, apparently ignoring what I am talking about 10001 and 10009.

conditionmet = ((df['AggRow'] <= 6001) | ((df['AggRow'] >= 10001) & (df['AggRow'] <= 10009)))

How do I make conditionmet = True if AggRow <= 6001 or AggRow and> = 10001 and <= 10009? Again, I would like to get an answer that uses similar syntax, if possible. Thank.

+4
source share
1 answer

Try using .loc, this will fill the "conditionmet" column with True, where your conditions are met.

df.loc[(df['AggRow'] <= 6001)
      |((df['AggRow'] <= 10009)
       &(df['AggRow'] >= 10001)), 'conditionmet'] = 'True'

NaN ( , ) "False", :

df.fillna('False')
+1

Source: https://habr.com/ru/post/1694068/


All Articles