This article mainly introduces how to use Pandas
Filter values 1.
Updated: 2022 / 6 / 21
See here 2
Dataframe
All rows in X = data[data['X'].isin(['A', 'B', 'C', 'D'])]
# Unamed:22 Unamed:23
# 561 NaN NaN NaN
# ... ... ... ...
# 1071 7.7 70805 NaN
# 1072 8.0 80851 NaN
#
# type(X)
# <class 'pandas.core.frame.DataFrame'>
#
# X.dtype
# AttributeError: 'DataFrame' object has no attribute 'dtype'
X = data.loc[data['X'].isin(['A', 'B', 'C', 'D'])]
Dataframe
All rows in X = data['X'].isin(['A', 'B', 'C', 'D'])
# 0 True
# 1 False
# ...
# 1076 True
# 1077 False
#
# type(X)
# <class 'pandas.core.series.Series'>
#
# X.dtype
# bool
See here 3
X = df[df[2].isna()]
# 0 NaN
# 1 NaN
# ... ...
# 105 NaN
# 106 NaN
#
# type(X)
# <class 'pandas.core.series.Series'>
#
# X[2].dtype
# object
df[df[2].notna()]
# 1869 ABCD
# 10712 EFGH
# ... ...
# 10723 IJK
# 10766 LMN
#
# type(X)
# <class 'pandas.core.series.Series'>
#
# X[2].dtype
# object
More about , You can see df.query
The method of data query is introduced in detail 4
df.query('Q1 > Q2 > 90') # Type of direct writing sql where sentence
df.query('Q1 + Q2 > 180')
df.query('Q1 == Q2')
df.query('(Q1<50) & (Q2>40) and (Q3>90)')
df.query('Q1 > Q2 > Q3 > Q4')
df.query('team != "C"')
df.query('team in ["A","B"]')
df.query('team not in ("E","A","B")')
df.query('team == ["A","B"]')
df.query('team != ["A","B"]')
df.query('name.str.contains("am")') # contain am character
See here 5
df.query('col1.isnull()', engine='python')
# col1
# 21 NaN
# 42 NaN
# ...
# 60 NaN
df.query('col1.notnull()', engine='python')
# col1
# 31 ABCD
# 32 EFGH
# ...
# 65 HIJK
# For columns with spaces in their names , You can use backquotes to enclose
df.query('B == `team name`')
# Support for incoming variables , Such as : Greater than average 40 Points of
a = df.Q1.mean()
df.query('Q1 > @a+40')
df.query('Q1 > `Q2`[email protected]')
# df.eval() Usage and df.query similar
df[df.eval("Q1 > 90 > Q3 > 10")]
df[df.eval("Q1 > `Q2`[email protected]")]
pandas Query filter data ︎
pandas adopt list Filter rows ︎
【pandas】dataframe According to whether a column is null Filter data ︎
pandas query() Expression query ︎
Querying for NaN and other names in Pandas︎