python自学开源工具技巧Pandas技巧

Pandas_Select_Data_query()

2020-03-29  本文已影响0人  Kaspar433

Pandas_Select_Data_query()

查询数据除了[ ]、loc、iloc、where外,还有另外一个简洁高效的查询方法——query()。

了解了一下相关操作,简直惊呆了,完全就是英语白话,容易掌握,语句也短,称为短小精悍一点儿都不为过。

学了之后,再也不用写辣么长的查询语句了。

本文就简单介绍一下query()方法的相关操作。

import pandas as pd
import numpy as np
​
data = pd.DataFrame(np.random.randn(5,4), columns=list('abcd'))
data

out:
    a   b   c   d
0   -0.055029   1.376917    -0.228314   1.595987
1   -0.259330   -0.114194   1.252481    0.386451
2   0.873330    -1.279337   2.390891    -0.044016
3   -1.190554   -1.359401   -0.191798   1.742165
4   -0.750102   0.143094    0.742452    -1.577230

pandas一般做法

data[(data.a < data.b) & (data.c > data.d)]

out:
    a   b   c   d
1   -0.259330   -0.114194   1.252481    0.386451
4   -0.750102   0.143094    0.742452    -1.577230
data.loc[(data.a < data.b) & (data.c > data.d)]

out:
    a   b   c   d
1   -0.259330   -0.114194   1.252481    0.386451
4   -0.750102   0.143094    0.742452    -1.577230

使用query()

data.query('(a < b) and (c > d)')

out:
    a   b   c   d
1   -0.259330   -0.114194   1.252481    0.386451
4   -0.750102   0.143094    0.742452    -1.577230
data.query('a < b < c')

out:
    a   b   c   d
1   -0.259330   -0.114194   1.252481    0.386451
4   -0.750102   0.143094    0.742452    -1.577230

query()可以使用索引

直接使用'index':

data.query('index > 2')

out:
    a   b   c   d
i               
3   -1.190554   -1.359401   -0.191798   1.742165
4   -0.750102   0.143094    0.742452    -1.577230

也可以先命名仔使用:

data.index.name= 'i'
data.query('i > 2')

out:
    a   b   c   d
i               
3   -1.190554   -1.359401   -0.191798   1.742165
4   -0.750102   0.143094    0.742452    -1.577230

如果索引的名称与列名称重叠,则列名称优先。

data.index.name= 'a'
data.query('a > .5')

out:
    a   b   c   d
a               
2   0.87333 -1.279337   2.390891    -0.044016

MultiIndex query()语法

companies = np.random.choice(['apple', 'huawei'], size = 6)
products = np.random.choice(['mobile', 'pad'], size = 6)
companies

out:
array(['apple', 'apple', 'huawei', 'huawei', 'huawei', 'huawei'],
      dtype='<U6')
products

out:
array(['pad', 'mobile', 'mobile', 'mobile', 'pad', 'pad'], dtype='<U6')
index = pd.MultiIndex.from_arrays([companies, products], names=['company', 'product'])
data_mul = pd.DataFrame(np.random.randn(6, 3), index=index)
data_mul

out:
                0   1   2
company product         
apple   pad -0.968198   -0.619911   1.134541
        mobile  -0.608740   -0.909656   -0.027498
huawei  mobile  1.016521    1.882203    -1.001023
        mobile  0.841595    0.065712    1.732188
        pad 0.540195    -0.762794   0.549758
        pad -2.613445   -0.387707   -0.780770
data_mul.query('product == "mobile"')


out:
                0   1   2
company product         
apple   mobile  -0.608740   -0.909656   -0.027498
huawei  mobile  1.016521    1.882203    -1.001023
        mobile  0.841595    0.065712    1.732188
data_mul.query('company == "huawei"')

out:
                0   1   2
company product         
huawei  mobile  1.016521    1.882203    -1.001023
        mobile  0.841595    0.065712    1.732188
        pad 0.540195    -0.762794   0.549758
        pad -2.613445   -0.387707   -0.780770

如果MultiIndex未命名的级别,您可以使用特殊名称引用它们:

data_mul.index.names = [None, None]
data_mul.query('ilevel_0 == "huawei"')

out:
                0   1   2
huawei  mobile  1.016521    1.882203    -1.001023
        mobile  0.841595    0.065712    1.732188
        pad 0.540195    -0.762794   0.549758
        pad -2.613445   -0.387707   -0.780770
data_mul.query('ilevel_1 == "pad"')

out:
            0   1   2
apple   pad -0.968198   -0.619911   1.134541
huawei  pad 0.540195    -0.762794   0.549758
        pad -2.613445   -0.387707   -0.780770

query()与pandas语法比较

有括号

data.query('(a < b) and (c > d)')

out:
    a   b   c   d
a               
1   -0.259330   -0.114194   1.252481    0.386451
4   -0.750102   0.143094    0.742452    -1.577230

无括号

data.query('a < b and c > d')

out:
    a   b   c   d
a               
1   -0.259330   -0.114194   1.252481    0.386451
4   -0.750102   0.143094    0.742452    -1.577230

使用符号

data.query('a < b & c > d')

out:
    a   b   c   d
a               
1   -0.259330   -0.114194   1.252481    0.386451
4   -0.750102   0.143094    0.742452    -1.577230

使用英语

data.query('a < b or c > d')

out:
    a   b   c   d
a               
0   -0.055029   1.376917    -0.228314   1.595987
1   -0.259330   -0.114194   1.252481    0.386451
2   0.873330    -1.279337   2.390891    -0.044016
4   -0.750102   0.143094    0.742452    -1.577230

接近自然语言

data.query('a < b < c')

out:
    a   b   c   d
a               
1   -0.259330   -0.114194   1.252481    0.386451
4   -0.750102   0.143094    0.742452    -1.577230

使用 in 与 not in 操作符

df = pd.DataFrame({'a': list('aabbccddeeff'), 'b': list('aaaabbbbcccc'),
                   'c': np.random.randint(5, size=12),
                   'd': np.random.randint(9, size=12)})
df

out:
    a   b   c   d
0   a   a   3   8
1   a   a   2   6
2   b   a   4   6
3   b   a   4   8
4   c   b   2   7
5   c   b   3   3
6   d   b   4   1
7   d   b   1   1
8   e   c   4   6
9   e   c   4   3
10  f   c   2   5
11  f   c   0   0

in

df.query('a in b')

out:
    a   b   c   d
0   a   a   3   8
1   a   a   2   6
2   b   a   4   6
3   b   a   4   8
4   c   b   2   7
5   c   b   3   3

一般方法

df[df.a.isin(df.b)]

out:
    a   b   c   d
0   a   a   3   8
1   a   a   2   6
2   b   a   4   6
3   b   a   4   8
4   c   b   2   7
5   c   b   3   3

not in

df.query('a not in b')

out: 
    a   b   c   d
6   d   b   4   1
7   d   b   1   1
8   e   c   4   6
9   e   c   4   3
10  f   c   2   5
11  f   c   0   0

一般方法:

df[~df.a.isin(df.b)]

out:
    a   b   c   d
6   d   b   4   1
7   d   b   1   1
8   e   c   4   6
9   e   c   4   3
10  f   c   2   5
11  f   c   0   0

与其他表达式结合

df.query('a in b and c < d')

out:
    a   b   c   d
0   a   a   3   8
1   a   a   2   6
2   b   a   4   6
3   b   a   4   8
4   c   b   2   7

一般方法:

df[(df.a.isin(df.b)) & (df.c < df.d)]

out:
    a   b   c   d
0   a   a   3   8
1   a   a   2   6
2   b   a   4   6
3   b   a   4   8
4   c   b   2   7

== 运算符与list的特殊用法

==、!=相当于in 、not in。

== 运算符

df.query('a == ["a", "b", "c"]')

out:
    a   b   c   d
0   a   a   3   8
1   a   a   2   6
2   b   a   4   6
3   b   a   4   8
4   c   b   2   7
5   c   b   3   3
df.query('a in ["a", "b", "c"]')

out:
    a   b   c   d
0   a   a   3   8
1   a   a   2   6
2   b   a   4   6
3   b   a   4   8
4   c   b   2   7
5   c   b   3   3

!= 运算符

df.query('a != ["a", "b", "c"]')

out:
    a   b   c   d
6   d   b   4   1
7   d   b   1   1
8   e   c   4   6
9   e   c   4   3
10  f   c   2   5
11  f   c   0   0
df.query('a not in ["a", "b", "c"]')

out:
    a   b   c   d
6   d   b   4   1
7   d   b   1   1
8   e   c   4   6
9   e   c   4   3
10  f   c   2   5
11  f   c   0   0

布尔运算符

可以使用单词not或~运算符否定布尔表达式。

df['bools'] = df.d > df.d.mean()
df.query('bools')

out:
    a   b   c   d   bools
0   a   a   3   8   True
1   a   a   2   6   True
2   b   a   4   6   True
3   b   a   4   8   True
4   c   b   2   7   True
8   e   c   4   6   True
10  f   c   2   5   True
df.query('d > d.mean()')

out:
    a   b   c   d   bools
0   a   a   3   8   True
1   a   a   2   6   True
2   b   a   4   6   True
3   b   a   4   8   True
4   c   b   2   7   True
8   e   c   4   6   True
10  f   c   2   5   True
df.query('not bools')

out:
    a   b   c   d   bools
5   c   b   3   3   False
6   d   b   4   1   False
7   d   b   1   1   False
9   e   c   4   3   False
11  f   c   0   0   False
df.query('~bools')

out:
    a   b   c   d   bools
5   c   b   3   3   False
6   d   b   4   1   False
7   d   b   1   1   False
9   e   c   4   3   False
11  f   c   0   0   False
​
上一篇 下一篇

猜你喜欢

热点阅读