根据大 pandas 列中的值从DataFrame中选择行 [英] Select rows from a DataFrame based on values in a column in pandas

查看:325
本文介绍了根据大 pandas 列中的值从DataFrame中选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据大熊猫某栏中的值从DataFrame中选择行?

在SQL中,我将使用:

  select * from table where colume_name = some_value。 

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

解决方案

要选择列值等于标量的行, 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' ] == some_value)& df ['other_column']。isin(some_values)] 






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

  df.loc [df ['column_name']!= some_value] 
/ pre>

isin 返回一个布尔系列,因此要选择其值为的行不是 some_values 中,使用取消布尔系列:

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






例如,

 将大熊猫导入为pd 
import numpy as np
df = pd.DataFrame({'A':'foo bar foo bar foo bar foo foo'.split(),
'B':'one one two three两个一个三'.split(),
'C':np.arange(8),'D':np.arange(8)* 2})
print(df)
#ABCD
#0 foo one 0 0
#1 ba r 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'])

  ABCD 
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'])])

  ABCD 
0 foo one 0 0
1 bar one 1 2
3 bar三3 6
6 foo one 6 12
7 foo three 7 14






请注意,如何永远,如果你想这么做很多次,那么
首先创建一个索引,然后使用 df.loc 来更有效:

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

产生

  ACD 
B
一个foo 0 0
一个酒吧1 2
一个foo 6 12

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

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

  ACD 
B
一个foo 0 0
一个酒吧1 2
两个foo 2 4
两个foo 4 8
两个酒吧5 10
一个foo 6 12


How to select rows from a DataFrame based on values in some column in pandas?
In SQL I would use:

select * from table where colume_name = some_value. 

I tried to look at pandas documentation but did not immediately find the answer.

解决方案

To select rows whose column value equals a scalar, some_value, use ==:

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

To select rows whose column value is in an iterable, some_values, use isin:

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

Combine multiple conditions with &:

df.loc[(df['column_name'] == some_value) & df['other_column'].isin(some_values)]


To select rows whose column value does not equal some_value, use !=:

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 ~:

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:

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 = 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.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

这篇关于根据大 pandas 列中的值从DataFrame中选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆