Filtering data in SQL mainly uses statements such as where, and, and in. In Python, there are many ways to implement screening, using AND & or | NOT ~ logical operators, and the isin() function. Let's feel the difference between the two:
Summary:
type | Python statement reference |
single condition filter | data[data['shop_type']=='A'] |
Filtering for multiple values | data[data['uid'].isin([17437864,17402857])] |
Filter without certain values | data[~data['AB_group'].isin(['A','B'])] |
And conditional filter | data[(data['shop_type']=='A')&(data['order_count']>=20)] |
or conditional screening | data[(data['order_count']>=100)|(data['order_shops']>=100)] |
Filtering by a single condition
Example: Screening Class A Stores
select
*
from t_store_order
where shop_type = 'A'
data[data['shop_type']=='A']
Detailed code:
== is used to judge whether two values are equal;
data['shop_type']=='A' returns Boolean data, and returns those rows whose value is True.
operation result:
isin() filtering within some range of values
Example: Query data for a given user id
select
*
from t_store_order
where uid in (17437864,17402857)
data[data['uid'].isin([17437864,17402857])]
Detailed code:
[17437864,17402857] is a list enclosed in square brackets;
The isin() method is used to determine whether a value is in a list, and returns a Boolean Series object;
data['uid'] is data in a Series format, and the isin() method can be called;
operation result:
Exclude certain values (filter without certain values)
Example: Eliminate data grouped into A and B
select
*
from t_store_order
where AB_group not in ('A','B')
data[~data['AB_group'].isin(['A','B'])]
Detailed code:
~ Represents the inversion operator, True will become False, and False will become True;
In the previous paragraph, we learned that data[~data['AB_group'].isin(['A','B'])] will generate a Boolean Series object. After adding the inversion operator, the values originally contained are It becomes to remove these values.
operation result:
query a range
Example: Query records with orders greater than 20
select
*
from t_store_order
where shop_type = 'A'
data[data['shop_type']=='A']
Multi-condition filter
and condition
Example: Query the records where the store type is A and the number of orders is greater than 20
select
*
from t_store_order
where shop_type = 'A'
and order_count >= 20
data[(data['shop_type']=='A')&(data['order_count']>=20)]
Detailed code:
& represents the AND operator;
In this way (Boolean Series object) & (Boolean Series object) two Boolean series perform an AND operation, and return True only if they are True at the same time;
operation result:
or condition
Example: Query records where the number of orders is greater than 100 or the number of stores where orders are placed is greater than 100
select
*
from t_store_order
where shop_type = 'A'
data[(data['order_count']>=100)|(data['order_shops']>=100)]
Detailed code:
| represents the or operator;
Two Boolean series are ANDed, and if one is True, it will return True, and if it is False, the result will be False;
operation result:
appendix:
The method of running SQL on jupyter notebook: comparing SQL to learning Python: running SQL on jupyter notebook_Zi Angzhang's Blog-CSDN Blog