如何通过query()函数在Python中实现高效查询?

2026-05-24 16:161阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1174个文字,预计阅读时间需要5分钟。

如何通过query()函数在Python中实现高效查询?

本篇文章与大家聊聊Python Pandas库的使用小技巧,介绍使用query()函数优雅查询的方法,希望对大家有所帮助!

对于Pandas数据条件获取指定数据,相信大家都能轻松写出对应的代码。以下是一个示例代码:

pythonimport pandas as pd

创建示例数据data={ 'name': ['Alice', 'Bob', 'Charlie', 'David'], 'age': [25, 30, 35, 40], 'salary': [5000, 6000, 7000, 8000]}

df=pd.DataFrame(data)

如何通过query()函数在Python中实现高效查询?

使用query()函数获取年龄大于30的数据filtered_data=df.query('age > 30')

print(filtered_data)

运行上述代码,将输出年龄大于30的员工信息:

name age salary

1Bob 30 6000

2Charlie 35 7000

3David 40 8000

本篇文章带大家聊聊一个Python Pandas库的使用小技巧,介绍一下使用query()优雅查询的方法,希望对大家有所帮助!

对于 Pandas 根据条件获取指定数据,相信大家都能够轻松的写出相应代码,但是如果你还没用过 query,相信你会被它的简洁所折服!

常规用法

先创建一个 DataFrame。

import pandas as pd df = pd.DataFrame( {'A': ['e', 'd', 'c', 'b', 'a'], 'B': ['f', 'b', 'c', 'd', 'e'], 'C': range(0, 10, 2), 'D': range(10, 0, -2), 'E.E': range(10, 5, -1)})

我们现在选取 A列字母出现在B列 的所有行。先看两种常见写法。

>>> df[df['A'].isin(df['B'])] A B C D E.E 0 e f 0 10 10 1 d b 2 8 9 2 c c 4 6 8 3 b d 6 4 7 >>> df.loc[df['A'].isin(df['B'])] A B C D E.E 0 e f 0 10 10 1 d b 2 8 9 2 c c 4 6 8 3 b d 6 4 7

下面使用 query() 来实现。

>>> df.query("A in B") A B C D E.E 0 e f 0 10 10 1 d b 2 8 9 2 c c 4 6 8 3 b d 6 4 7

可以看到使用 query 后的代码简洁易懂,并且它对于内存的消耗也更小。

多条件查询

选取 A列字母出现在B列,并且C列小于D列 的所有行。

>>> df.query('A in B and C < D') A B C D E.E 0 e f 0 10 10 1 d b 2 8 9 2 c c 4 6 8

这里 and 也可以用 & 表示。

引用变量

表达式中也可以使用外部定义的变量,在变量名前用@标明。

>>> number = 5 >>> df.query('A in B & C > @number') A B C D E.E 3 b d 6 4 7

索引选取

选取 A列字母出现在B列,并且索引大于2 的所有行。

>>> df.query('A in B and index > 2') A B C D E.E 3 b d 6 4 7

多索引选取

创建一个两层索引的 DataFrame。

>>> import numpy as np >>> colors = ['yellow']*3 + ['red']*2 >>> rank = [str(i) for i in range(5)] >>> index = pd.MultiIndex.from_arrays([colors, rank], names=['color', 'rank']) >>> df = pd.DataFrame(np.arange(10).reshape(5, 2),columns=['A', 'B'] , index=index) >>> df = pd.DataFrame(np.arange(10).reshape(5, 2),columns=['A', 'B'] , index=index) >>> df A B color rank yellow 0 0 1 1 2 3 2 4 5 red 3 6 7 4 8 9

1、当有多层索引有名称时,通过索引名称直接选取。

>>> df.query("color == 'red'") A B color rank red 3 6 7 4 8 9

2、当有多层索引无名时,通过索引级别来选取。

>>> df.index.names = [None, None] >>> df.query("ilevel_0 == 'red'") A B red 3 6 7 4 8 9 >>> df.query("ilevel_1 == '4'") A B red 4 8 9

特殊字符

对于列名中间有空格或运算符等其他特殊符号,需要使用反引号 ``

>>> df.query('A == B | (C + 2 > `E.E`)') A B C D E.E 2 c c 4 6 8 3 b d 6 4 7 4 a e 8 2 6

总的来说,query() 用法比较简单,可以快速上手,代码可读性也提高了不少。

以上就是一文了解Python中如何使用query()进行优雅的查询的详细内容,更多请关注自由互联其它相关文章!

本文共计1174个文字,预计阅读时间需要5分钟。

如何通过query()函数在Python中实现高效查询?

本篇文章与大家聊聊Python Pandas库的使用小技巧,介绍使用query()函数优雅查询的方法,希望对大家有所帮助!

对于Pandas数据条件获取指定数据,相信大家都能轻松写出对应的代码。以下是一个示例代码:

pythonimport pandas as pd

创建示例数据data={ 'name': ['Alice', 'Bob', 'Charlie', 'David'], 'age': [25, 30, 35, 40], 'salary': [5000, 6000, 7000, 8000]}

df=pd.DataFrame(data)

如何通过query()函数在Python中实现高效查询?

使用query()函数获取年龄大于30的数据filtered_data=df.query('age > 30')

print(filtered_data)

运行上述代码,将输出年龄大于30的员工信息:

name age salary

1Bob 30 6000

2Charlie 35 7000

3David 40 8000

本篇文章带大家聊聊一个Python Pandas库的使用小技巧,介绍一下使用query()优雅查询的方法,希望对大家有所帮助!

对于 Pandas 根据条件获取指定数据,相信大家都能够轻松的写出相应代码,但是如果你还没用过 query,相信你会被它的简洁所折服!

常规用法

先创建一个 DataFrame。

import pandas as pd df = pd.DataFrame( {'A': ['e', 'd', 'c', 'b', 'a'], 'B': ['f', 'b', 'c', 'd', 'e'], 'C': range(0, 10, 2), 'D': range(10, 0, -2), 'E.E': range(10, 5, -1)})

我们现在选取 A列字母出现在B列 的所有行。先看两种常见写法。

>>> df[df['A'].isin(df['B'])] A B C D E.E 0 e f 0 10 10 1 d b 2 8 9 2 c c 4 6 8 3 b d 6 4 7 >>> df.loc[df['A'].isin(df['B'])] A B C D E.E 0 e f 0 10 10 1 d b 2 8 9 2 c c 4 6 8 3 b d 6 4 7

下面使用 query() 来实现。

>>> df.query("A in B") A B C D E.E 0 e f 0 10 10 1 d b 2 8 9 2 c c 4 6 8 3 b d 6 4 7

可以看到使用 query 后的代码简洁易懂,并且它对于内存的消耗也更小。

多条件查询

选取 A列字母出现在B列,并且C列小于D列 的所有行。

>>> df.query('A in B and C < D') A B C D E.E 0 e f 0 10 10 1 d b 2 8 9 2 c c 4 6 8

这里 and 也可以用 & 表示。

引用变量

表达式中也可以使用外部定义的变量,在变量名前用@标明。

>>> number = 5 >>> df.query('A in B & C > @number') A B C D E.E 3 b d 6 4 7

索引选取

选取 A列字母出现在B列,并且索引大于2 的所有行。

>>> df.query('A in B and index > 2') A B C D E.E 3 b d 6 4 7

多索引选取

创建一个两层索引的 DataFrame。

>>> import numpy as np >>> colors = ['yellow']*3 + ['red']*2 >>> rank = [str(i) for i in range(5)] >>> index = pd.MultiIndex.from_arrays([colors, rank], names=['color', 'rank']) >>> df = pd.DataFrame(np.arange(10).reshape(5, 2),columns=['A', 'B'] , index=index) >>> df = pd.DataFrame(np.arange(10).reshape(5, 2),columns=['A', 'B'] , index=index) >>> df A B color rank yellow 0 0 1 1 2 3 2 4 5 red 3 6 7 4 8 9

1、当有多层索引有名称时,通过索引名称直接选取。

>>> df.query("color == 'red'") A B color rank red 3 6 7 4 8 9

2、当有多层索引无名时,通过索引级别来选取。

>>> df.index.names = [None, None] >>> df.query("ilevel_0 == 'red'") A B red 3 6 7 4 8 9 >>> df.query("ilevel_1 == '4'") A B red 4 8 9

特殊字符

对于列名中间有空格或运算符等其他特殊符号,需要使用反引号 ``

>>> df.query('A == B | (C + 2 > `E.E`)') A B C D E.E 2 c c 4 6 8 3 b d 6 4 7 4 a e 8 2 6

总的来说,query() 用法比较简单,可以快速上手,代码可读性也提高了不少。

以上就是一文了解Python中如何使用query()进行优雅的查询的详细内容,更多请关注自由互联其它相关文章!