Applying AND, OR, NOT conditions as filter into dataframe

Question:

How to add conditions into data frame filters, to express the function (AND, OR, NOT)? For example, I have two flags:

  1. myData flag to be myData_flag
  2. myProx flag to be is_myProx_t_f.

Conditions are defined as below:

  • AND: is it data_myDatamyProx = data[(data[‘myData_flag’].isin([‘1’]),:)&&( data[‘is_myProx_t_f’].isin([‘1’]),:)]?
  • OR: is it data_myDataOrmyProx = data[(data[‘myData_flag’].isin([‘1’]),:)||( data[‘is_myProx_t_f’].isin([‘1’]),:)]?
  • NOT: is it data_NonemyDatamyProx = data[(data[‘myData_flag’].isnotin([‘1’]),:)||( data[‘is_myProx_t_f’].isnotin([‘1’]),:)]?

Solution:

For AND, OR operators, you can already accomplish this like below (using iris dataset as an example):
df[(df['Sepal.Length'] < 5) & (df['Sepal.Width'] > 3) | (df['Species'].isin(['setosa'])), :]
Above, the operators are &, | and negation is the tilda ~ .
Thats it, enjoy!!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s