如何根据Pandas中某列的值从DataFrame中选择行?

在SQL中,我会使用:

SELECT *
FROM table
WHERE column_name = some_value

当前回答

1.在query()调用中使用f-string

如果用于过滤数据帧的列名来自本地变量,则f-string可能有用。例如

col = 'A'
df.query(f"{col} == 'foo'")

事实上,f-string也可以用于查询变量(datetime除外):

col = 'A'
my_var = 'foo'
df.query(f"{col} == '{my_var}'") # if my_var is a string

my_num = 1
df.query(f"{col} == {my_num}") # if my_var is a number

my_date = '2022-12-10'
df.query(f"{col} == @my_date") # must use @ for datetime though

2.安装numexpr以加快query()调用

panda文档建议在使用query()时安装numexpr以加速数值计算。使用pipinstallnumexpr(或conda、sudo等,具体取决于您的环境)来安装它。

对于更大的数据帧(性能非常重要),带有numexpr引擎的df.query()比df[mask]执行得更快。特别是,它在以下情况下表现更好。

字符串列上的逻辑和/或比较运算符

如果将一列字符串与其他字符串进行比较,并且要选择匹配的行,即使是单个比较操作,query()的执行速度也比df[mask]快。例如,对于具有80k行的数据帧,速度快30%1,对于具有800k行的数据框架,速度快60%。2

df[df.A == 'foo']
df.query("A == 'foo'")  # <--- performs 30%-60% faster

这一差距随着操作数量的增加而增加(如果链接了4个比较df.query()比df[mask]快2-2.3倍)1,2和/或数据帧长度的增加而增大。2

数字列上的多个操作

如果需要计算多个算术、逻辑或比较操作来创建布尔掩码以过滤df,则query()执行速度更快。例如,对于一个有80k行的帧,它的速度快20%1,而对于一个800k行的帧来说,速度快2倍。2

df[(df.B % 5) **2 < 0.1]
df.query("(B % 5) **2 < 0.1")  # <--- performs 20%-100% faster.

随着操作数量的增加和/或数据帧长度的增加,性能差距也会增加。2

下图显示了随着数据帧长度的增加,这些方法的性能。3

3.在query()中调用panda方法

Numexpr当前仅支持逻辑(&,|,~)、比较(==,>,<,>=,<=,!=)和基本算术运算符(+,-,*,/,**,%)。

例如,它不支持整数除法(//)。然而,调用等效的panda方法(floordiv())是有效的。

df.query('B.floordiv(2) <= 3')  # or 
df.query('B.floordiv(2).le(3)')

# for pandas < 1.4, need `.values`
df.query('B.floordiv(2).values <= 3')


1使用80k行框架的基准代码

import numpy as np
df = pd.DataFrame({'A': 'foo bar foo baz foo bar foo foo'.split()*10000, 
                   'B': np.random.rand(80000)})

%timeit df[df.A == 'foo']
# 8.5 ms ± 104.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit df.query("A == 'foo'")
# 6.36 ms ± 95.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit df[((df.A == 'foo') & (df.A != 'bar')) | ((df.A != 'baz') & (df.A != 'buz'))]
# 29 ms ± 554 µs per loop (mean ± std. dev. of 10 runs, 100 loops each)
%timeit df.query("A == 'foo' & A != 'bar' | A != 'baz' & A != 'buz'")
# 16 ms ± 339 µs per loop (mean ± std. dev. of 10 runs, 100 loops each)

%timeit df[(df.B % 5) **2 < 0.1]
# 5.35 ms ± 37.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit df.query("(B % 5) **2 < 0.1")
# 4.37 ms ± 46.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

2使用800k行框架的基准代码

df = pd.DataFrame({'A': 'foo bar foo baz foo bar foo foo'.split()*100000, 
                   'B': np.random.rand(800000)})

%timeit df[df.A == 'foo']
# 87.9 ms ± 873 µs per loop (mean ± std. dev. of 10 runs, 100 loops each)
%timeit df.query("A == 'foo'")
# 54.4 ms ± 726 µs per loop (mean ± std. dev. of 10 runs, 100 loops each)

%timeit df[((df.A == 'foo') & (df.A != 'bar')) | ((df.A != 'baz') & (df.A != 'buz'))]
# 310 ms ± 3.4 ms per loop (mean ± std. dev. of 10 runs, 100 loops each)
%timeit df.query("A == 'foo' & A != 'bar' | A != 'baz' & A != 'buz'")
# 132 ms ± 2.43 ms per loop (mean ± std. dev. of 10 runs, 100 loops each)

%timeit df[(df.B % 5) **2 < 0.1]
# 54 ms ± 488 µs per loop (mean ± std. dev. of 10 runs, 100 loops each)
%timeit df.query("(B % 5) **2 < 0.1")
# 26.3 ms ± 320 µs per loop (mean ± std. dev. of 10 runs, 100 loops each)

3:用于生成字符串和数字的两种方法的性能图的代码。

from perfplot import plot
constructor = lambda n: pd.DataFrame({'A': 'foo bar foo baz foo bar foo foo'.split()*n, 'B': np.random.rand(8*n)})
plot(
    setup=constructor,
    kernels=[lambda df: df[(df.B%5)**2<0.1], lambda df: df.query("(B%5)**2<0.1")],
    labels= ['df[(df.B % 5) **2 < 0.1]', 'df.query("(B % 5) **2 < 0.1")'],
    n_range=[2**k for k in range(4, 24)],
    xlabel='Rows in DataFrame',
    title='Multiple mathematical operations on numbers',
    equality_check=pd.DataFrame.equals);
plot(
    setup=constructor,
    kernels=[lambda df: df[df.A == 'foo'], lambda df: df.query("A == 'foo'")],
    labels= ["df[df.A == 'foo']", """df.query("A == 'foo'")"""],
    n_range=[2**k for k in range(4, 24)],
    xlabel='Rows in DataFrame',
    title='Comparison operation on strings',
    equality_check=pd.DataFrame.equals);

其他回答

要选择列值等于标量some_value的行,请使用==:

df.loc[df['column_name'] == some_value]

要选择列值在可迭代的some_values中的行,请使用isin:

df.loc[df['column_name'].isin(some_values)]

将多个条件与&组合:

df.loc[(df['column_name'] >= A) & (df['column_name'] <= B)]

注意括号。由于Python的运算符优先级规则,&binding比<=和>=更紧密。因此,最后一个示例中的括号是必要的。没有括号

df['column_name'] >= A & df['column_name'] <= B

解析为

df['column_name'] >= (A & df['column_name']) <= B

这导致序列的真值是模糊错误。


要选择列值不等于some_value的行,请使用!=:

df.loc[df['column_name'] != some_value]

isin返回布尔级数,因此要选择值不在some_values中的行,请使用~:

df.loc[~df['column_name'].isin(some_values)]

例如

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})
print(df)
#      A      B  C   D
# 0  foo    one  0   0
# 1  bar    one  1   2
# 2  foo    two  2   4
# 3  bar  three  3   6
# 4  foo    two  4   8
# 5  bar    two  5  10
# 6  foo    one  6  12
# 7  foo  three  7  14

print(df.loc[df['A'] == 'foo'])

产量

     A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14

如果要包含多个值,请将它们放入列出(或更一般地,任何可迭代的)并使用isin:

print(df.loc[df['B'].isin(['one','three'])])

产量

     A      B  C   D
0  foo    one  0   0
1  bar    one  1   2
3  bar  three  3   6
6  foo    one  6  12
7  foo  three  7  14

但是,请注意,如果您希望多次这样做首先创建索引,然后使用df.loc:

df = df.set_index(['B'])
print(df.loc['one'])

产量

       A  C   D
B              
one  foo  0   0
one  bar  1   2
one  foo  6  12

或者,要包含索引中的多个值,请使用df.index.isin:

df.loc[df.index.isin(['one','two'])]

产量

       A  C   D
B              
one  foo  0   0
one  bar  1   2
two  foo  2   4
two  foo  4   8
two  bar  5  10
one  foo  6  12

要添加:您还可以执行df.groupby('column_name').get_group('column_desired_value').reset_index()以生成具有特定值的指定列的新数据帧。例如。,

import pandas as pd
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split()})
print("Original dataframe:")
print(df)

b_is_two_dataframe = pd.DataFrame(df.groupby('B').get_group('two').reset_index()).drop('index', axis = 1) 
#NOTE: the final drop is to remove the extra index column returned by groupby object
print('Sub dataframe where B is two:')
print(b_is_two_dataframe)

运行此命令可以:

Original dataframe:
     A      B
0  foo    one
1  bar    one
2  foo    two
3  bar  three
4  foo    two
5  bar    two
6  foo    one
7  foo  three
Sub dataframe where B is two:
     A    B
0  foo  two
1  foo  two
2  bar  two

您可以在函数中使用loc(方括号):

# Series
s = pd.Series([1, 2, 3, 4]) 
s.loc[lambda x: x > 1]
# s[lambda x: x > 1]

输出:

1    2
2    3
3    4
dtype: int64

or

# DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [10, 20, 30]})
df.loc[lambda x: x['A'] > 1]
# df[lambda x: x['A'] > 1]

输出:

   A   B
1  2  20
2  3  30

tl;博士

熊猫相当于

select * from table where column_name = some_value

is

table[table.column_name == some_value]

多种条件:

table[(table.column_name == some_value) | (table.column_name2 == some_value2)]

or

table.query('column_name == some_value | column_name2 == some_value2')

代码示例

import pandas as pd

# Create data set
d = {'foo':[100, 111, 222],
     'bar':[333, 444, 555]}
df = pd.DataFrame(d)

# Full dataframe:
df

# Shows:
#    bar   foo
# 0  333   100
# 1  444   111
# 2  555   222

# Output only the row(s) in df where foo is 222:
df[df.foo == 222]

# Shows:
#    bar  foo
# 2  555  222

在上面的代码中,是df[df.foo==222]行根据列值给出行,在本例中为222。

也可能出现多种情况:

df[(df.foo == 222) | (df.bar == 444)]
#    bar  foo
# 1  444  111
# 2  555  222

但在这一点上,我建议使用查询函数,因为它不那么冗长,并产生相同的结果:

df.query('foo == 222 | bar == 444')

下面是一个简单的例子

from pandas import DataFrame

# Create data set
d = {'Revenue':[100,111,222], 
     'Cost':[333,444,555]}
df = DataFrame(d)


# mask = Return True when the value in column "Revenue" is equal to 111
mask = df['Revenue'] == 111

print mask

# Result:
# 0    False
# 1     True
# 2    False
# Name: Revenue, dtype: bool


# Select * FROM df WHERE Revenue = 111
df[mask]

# Result:
#    Cost    Revenue
# 1  444     111