本文翻译自:How to select rows from a DataFrame based on column values?

How to select rows from a DataFrame based on values in some column in Python Pandas? 如何基于Python Pandas中某些列中的值从DataFrame选择行?

In SQL, I would use: 在SQL中,我将使用:

SELECT *
FROM table
WHERE colume_name = some_value

I tried to look at pandas documentation but did not immediately find the answer. 我试图查看熊猫文档,但没有立即找到答案。


#1楼

参考:https://stackoom.com/question/19dAl/如何基于列值从DataFrame中选择行


#2楼

To select rows whose column value equals a scalar, some_value , use == : 要选择列值等于标量some_value ,请使用==

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

To select rows whose column value is in an iterable, some_values , use isin : 要选择行其列值是一个迭代, some_values ,使用isin

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

Combine multiple conditions with & : 将多个条件与&组合:

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

Note the parentheses. 注意括号。 Due to Python's operator precedence rules , & binds more tightly than <= and >= . 由于Python的运算符优先级规则 , &绑定比<=>=更紧密。 Thus, the parentheses in the last example are necessary. 因此,最后一个示例中的括号是必需的。 Without the parentheses 没有括号

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

is parsed as 被解析为

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

which results in a Truth value of a Series is ambiguous error . 这导致一个系列的真值是模棱两可的错误 。


To select rows whose column value does not equal some_value , use != : 要选择列值不等于 some_value ,请使用!=

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

isin returns a boolean Series, so to select rows whose value is not in some_values , negate the boolean Series using ~ : isin返回一个布尔系列,因此要选择值不在 some_values行,请使用~取反布尔系列:

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

For example, 例如,

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  14print(df.loc[df['A'] == 'foo'])

yields 产量

     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

If you have multiple values you want to include, put them in a list (or more generally, any iterable) and use isin : 如果要包含多个值,请将它们放在列表中(或更普遍地说,是任何可迭代的),然后使用isin

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

yields 产量

     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

Note, however, that if you wish to do this many times, it is more efficient to make an index first, and then use df.loc : 但是请注意,如果您希望多次执行此操作,则首先创建索引,然后使用df.loc会更有效:

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

yields 产量

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

or, to include multiple values from the index use df.index.isin : 或者,要包含索引中的多个值,请使用df.index.isin

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

yields 产量

       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

#3楼

Here is a simple example 这是一个简单的例子

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'] == 111print 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

#4楼

tl;dr tl; dr

The pandas equivalent to 大熊猫相当于

select * from table where column_name = some_value

is

table[table.column_name == some_value]

Multiple conditions: 多个条件:

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

or 要么

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

Code example 代码示例

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

In the above code it is the line df[df.foo == 222] that gives the rows based on the column value, 222 in this case. 在上面的代码它是线df[df.foo == 222]给出基于列的值,行222在这种情况下。

Multiple conditions are also possible: 多种条件也是可能的:

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

But at that point I would recommend using the query function, since it's less verbose and yields the same result: 但是在那一点上,我建议使用查询函数,因为它不太冗长,并且产生的结果相同:

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

#5楼

I find the syntax of the previous answers to be redundant and difficult to remember. 我发现先前答案的语法是多余的,很难记住。 Pandas introduced the query() method in v0.13 and I much prefer it. 熊猫在v0.13中引入了query()方法,我更喜欢它。 For your question, you could do df.query('col == val') 对于您的问题,您可以执行df.query('col == val')

Reproduced from http://pandas.pydata.org/pandas-docs/version/0.17.0/indexing.html#indexing-query 转载自http://pandas.pydata.org/pandas-docs/version/0.17.0/indexing.html#indexing-query

In [167]: n = 10In [168]: df = pd.DataFrame(np.random.rand(n, 3), columns=list('abc'))In [169]: df
Out[169]: a         b         c
0  0.687704  0.582314  0.281645
1  0.250846  0.610021  0.420121
2  0.624328  0.401816  0.932146
3  0.011763  0.022921  0.244186
4  0.590198  0.325680  0.890392
5  0.598892  0.296424  0.007312
6  0.634625  0.803069  0.123872
7  0.924168  0.325076  0.303746
8  0.116822  0.364564  0.454607
9  0.986142  0.751953  0.561512# pure python
In [170]: df[(df.a < df.b) & (df.b < df.c)]
Out[170]: a         b         c
3  0.011763  0.022921  0.244186
8  0.116822  0.364564  0.454607# query
In [171]: df.query('(a < b) & (b < c)')
Out[171]: a         b         c
3  0.011763  0.022921  0.244186
8  0.116822  0.364564  0.454607

You can also access variables in the environment by prepending an @ . 您还可以在环境中添加@来访问变量。

exclude = ('red', 'orange')
df.query('color not in @exclude')

#6楼

To append to this famous question (though a bit too late): You can also do df.groupby('column_name').get_group('column_desired_value').reset_index() to make a new data frame with specified column having a particular value. 附加到这个著名的问题(虽然为时已晚):您还可以执行df.groupby('column_name').get_group('column_desired_value').reset_index()来创建一个具有指定值的具有特定值的新数据框。 Eg 例如

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)

Run this gives: 运行此给出:

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

如何基于列值从DataFrame中选择行?相关推荐

  1. pandas基于条件判断更新dataframe中所有数据列数值内容的值(Conditionally updating all values in pandas Dataframe )

    pandas基于条件判断更新dataframe中所有数据列数值内容的值(Conditionally updating all values in pandas Dataframe ) 目录

  2. pandas基于条件判断更新dataframe中特定数据列数值内容的值(Conditionally updating values in specific pandas Dataframe )

    pandas基于条件判断更新dataframe中特定数据列数值内容的值(Conditionally updating values in specific pandas Dataframe ) 目录

  3. R语言dplyr包的mutate函数将列添加到dataframe中或者修改现有的数据列:基于条件判断创建布尔型指示变量、将异常离散编码转化为NA值

    R语言dplyr包的mutate函数将列添加到dataframe中或者修改现有的数据列:基于条件判断创建布尔型指示变量.将异常离散编码转化为NA值 目录

  4. R语言dplyr包的mutate函数将列添加到dataframe中或者修改现有的数据列:使用na_if()函数将0值替换为NA值、负收入替换为NA值

    R语言dplyr包的mutate函数将列添加到dataframe中或者修改现有的数据列:使用na_if()函数将0值替换为NA值.负收入替换为NA值 目录

  5. 根据列值删除Pandas中的DataFrame行

    本文翻译自:Deleting DataFrame row in Pandas based on column value I have the following DataFrame: 我有以下Dat ...

  6. pandas使用query函数基于判断条件获得dataframe中满足条件的数据行(row)的索引列表(index of rows matching conditions in dataframe)

    pandas使用query函数基于判断条件获得dataframe中满足条件的数据行(row)的索引列表(index of rows matching conditions in dataframe) ...

  7. 如何在Pandas的DataFrame中的行上进行迭代?

    本文翻译自:How to iterate over rows in a DataFrame in Pandas? I have a DataFrame from pandas: 我有一个来自熊猫的Da ...

  8. pandas使用query函数基于组合索引筛选dataframe的数据行(与and、或or、非not)

    pandas使用query函数基于组合索引筛选dataframe的数据行(与and.或or.非not) 目录 pandas使用query函数基于组合索引筛选dataframe的数据行(与and.或or ...

  9. Excel 中选择行的快捷方式有哪些

    在处理不同类型的数据时,用户需要多次选择任意行或多行.在这种情况下,任何人都必须知道选择行的快捷方式.了解一些快捷键,如在Excel中选择行,使任何人都能熟练使用MS Excel,而且这对任何人以更智 ...

最新文章

  1. 汇编语言随笔(2)-CPU指令周期和无条件转移指令(包括call和ret指令)
  2. Windows内核符号表学习总结
  3. C#编程总结(七)数据加密——附源码
  4. 图像抠图Image Matting算法调研
  5. jvm锁_JVM如何处理锁
  6. 流星数据恢复软件 v2.4
  7. IDEA—使用技巧总结 [持续更新... ...]
  8. c++ 哈希_Redis源码解析十一--Hash键实现Redis 哈希键命令实现(t_hash)
  9. 深入出不来nodejs源码-内置模块引入初探
  10. 【SCIR AAAI2021】数据增强没效果?试试用Cluster-to-Cluster生成更多样化的新数据吧...
  11. SmartPLS软件如何做有调节的中介作用模型?
  12. WRF users guide Chap5
  13. ellipse函数用法
  14. [转]关于模糊控制的理解
  15. Alibaba Cloud Linux 3京东青龙面板搭建踩坑
  16. daterangepicker.js快捷筛选项的高亮显示问题修复
  17. 全国考研计算机成绩排名,计算机考研模拟考试全国排行
  18. mpvue使用vuex基本步骤以及如何使用
  19. 海康威视SDK控制台程序consoleDemo
  20. 李弘毅机器学习:第四章—梯度下降法

热门文章

  1. javascript三元操作符
  2. Unity3d之Http通讯GET方法和POST方法
  3. 2015 多校联赛 ——HDU5344(水)
  4. 禁用并删除 Wordpress 文章修订(revision)记录
  5. js基础之--变量 作用域和内存问题
  6. 基础算法---二分查找
  7. vs2008 MFC类继承结构
  8. Shell脚本实战:日志关键字监控+自动告警
  9. Ubuntu 18.04环境下Django的安装配置图文详解
  10. vim下中文乱码问题解决办法