在这个Python教程中,我们将讨论如何使用DataFrame.query()函数来查询pandas DataFrames。那么,让我们开始讨论吧。
pandas中DataFrame.query()函数的语法
pandas.DataFrame.query(expr, inplace=False, **kwargs)
expr= 它是一个字符串,包含逻辑表达式,根据该表达式选择pandas DataFrame的行(当expr的值=True时)。
inplace= 它是一个布尔值(要么是'True',要么是'False'),将决定DataFrame是原地修改还是返回修改后的DataFrame的新副本。
**kwargs= 如果有的话,它指的是其他关键字参数。
什么时候使用DataFrame.query()函数?
Pandas为我们提供了许多方式/方法来选择或过滤pandas DataFrame对象中的行。而pandas中的DataFrame.query() 函数是过滤pandas DataFrame对象中的行的有力方法之一。
使用DataFrame.query() 函数来选择或过滤pandas DataFrame对象中的行,而不是使用传统的和常用的索引方法,是比较可取的。这个DataFrame.query() 函数也可以和其他的pandas方法一起使用,使数据操作更加顺畅和直接。
DataFrame.query()函数的例子
让我们创建一个样本的pandas DataFrame对象来工作,并尝试在一些例子的帮助下理解DataFrame.query() 函数的功能/工作。
创建一个样本的pandas DataFrame对象
# Import pandas Python module
import pandas as pd
# Create a pandas DataFrame object
df = pd.DataFrame({'Dept': ['ECE', 'ICE', 'IT', 'CSE', 'CHE', 'EE', 'TE', 'ME', 'CSE', 'IPE', 'ECE'],
'GPA': [8.85, 9.03, 7.85, 8.85, 9.45, 7.45, 6.85, 9.35, 6.53,8.85, 7.83],
'Name': ['Mohan', 'Gautam', 'Tanya', 'Rashmi', 'Kirti', 'Ravi', 'Sanjay', 'Naveen', 'Gaurav', 'Ram', 'Tom'],
'RegNo': [111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121],
'City': ['Biharsharif','Ranchi','Patna','Patiala','Rajgir','Patna','Patna','Mysore','Patna','Mumbai','Patna']})
# Print the created pandas DataFrame
print('Sample pandas DataFrame:\n')
print(df)
输出
Sample pandas DataFrame:
Dept GPA Name RegNo City
0 ECE 8.85 Mohan 111 Biharsharif
1 ICE 9.03 Gautam 112 Ranchi
2 IT 7.85 Tanya 113 Patna
3 CSE 8.85 Rashmi 114 Patiala
4 CHE 9.45 Kirti 115 Rajgir
5 EE 7.45 Ravi 116 Patna
6 TE 6.85 Sanjay 117 Patna
7 ME 9.35 Naveen 118 Mysore
8 CSE 6.53 Gaurav 119 Patna
9 IPE 8.85 Ram 120 Mumbai
10 ECE 7.83 Tom 121 Patna
例子 #1
选择样本DataFrame的行,其中(City = "Patna")。
# Filter the rows of the sample DataFrame which has City = 'Patna'
# Using the DataFrame.query() function
df2 = df.query('City=="Patna"')
# Print the filtered sample pandas DataFrame
print('Filtered sample pandas DataFrame:\n')
print(df2)
输出
Filtered sample pandas DataFrame:
Dept GPA Name RegNo City
2 IT 7.85 Tanya 113 Patna
5 EE 7.45 Ravi 116 Patna
6 TE 6.85 Sanjay 117 Patna
8 CSE 6.53 Gaurav 119 Patna
10 ECE 7.83 Tom 121 Patna
例子 #2
选择样本数据框架中(GPA<8)的行。
# Filter the rows of the sample DataFrame which has GPA < 8
# Using the DataFrame.query() function
df2 = df.query('GPA < 8' & City == "Patna")
# Print the filtered sample pandas DataFrame
print('Filtered sample pandas DataFrame:\n')
print(df2)
输出
Filtered sample pandas DataFrame:
Dept GPA Name RegNo City
2 IT 7.85 Tanya 113 Patna
5 EE 7.45 Ravi 116 Patna
6 TE 6.85 Sanjay 117 Patna
8 CSE 6.53 Gaurav 119 Patna
10 ECE 7.83 Tom 121 Patna
例子#3
选择样本DataFrame中的行,其中(GPA < 7 and City = 'Patna')。
# Filter the rows of the sample DataFrame which has GPA < 7 & City = 'Patna'
# Using the DataFrame.query() function
df2 = df.query('GPA < 7 & City == "Patna"')
# Print the filtered sample pandas DataFrame
print('Filtered sample pandas DataFrame:\n')
print(df2)
输出结果
Filtered sample pandas DataFrame:
Dept GPA Name RegNo City
6 TE 6.85 Sanjay 117 Patna
8 CSE 6.53 Gaurav 119 Patna
例子 #4
选择样本数据框架的行,该数据框架的部门为[ECE, CSE, IT]。
# Filter the rows of the sample DataFrame which has Dept in (ECE, CSE, IT)
# Using the DataFrame.query() function
df2 = df.query("Dept in ['CSE','ECE','IT']")
# Print the filtered sample pandas DataFrame
print('Filtered sample pandas DataFrame:\n')
print(df2)
输出结果
Filtered sample pandas DataFrame:
Dept GPA Name RegNo City
0 ECE 8.85 Mohan 111 Biharsharif
2 IT 7.85 Tanya 113 Patna
3 CSE 8.85 Rashmi 114 Patiala
8 CSE 6.53 Gaurav 119 Patna
10 ECE 7.83 Tom 121 Patna
例子#5
选择样本数据框架中的行,其中(RegNo < 115 and GPA > 7)。
# Filter the rows of the sample DataFrame which has (RegNo < 115 & GPA > 7)
# Using the DataFrame.query() function
df2 = df.query("RegNo < 115 & GPA > 7")
# Print the filtered sample pandas DataFrame
print('Filtered sample pandas DataFrame:\n')
print(df2)
输出
Filtered sample pandas DataFrame:
Dept GPA Name RegNo City
0 ECE 8.85 Mohan 111 Biharsharif
1 ICE 9.03 Gautam 112 Ranchi
2 IT 7.85 Tanya 113 Patna
3 CSE 8.85 Rashmi 114 Patiala
总结
在这个Python教程中,我们已经学会了如何使用Pandas中的DataFrame.query() 函数来查询我们的pandas DataFrame对象。希望你已经理解了上面讨论的概念和例子,并准备使用它们来查询你自己的pandas DataFrame。谢谢你的阅读!请继续关注我们,了解更多关于Python编程的精彩学习内容。