Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

python - 如何基于列值从DataFrame中选择行?(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.

(我试图查看熊猫文档,但没有立即找到答案。)

  ask by szli translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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  14

print(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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...