简体   繁体   English

如何根据Python中的简单条件过滤熊猫数据框?

[英]How do I filter a Panda dataframe based on a simple criteria in Python?

Simple Question! 简单的问题!

I have a Panda Dataframe. 我有一个熊猫数据框。

How do I create a new array (dataframe, numpy array or list) that selects all the data on the original dataframe with the value "0" on the first column. 如何创建一个新数组(数据帧,numpy数组或列表),以选择第一列中值为“ 0”的原始数据帧中的所有数据。

For example If I had: 例如,如果我有:

0, 10, 15
0, 5, 16
2, 5, 9
1, 2, 2

I will have a new filtered list: 我将有一个新的过滤列表:

0, 10, 15
0, 5, 16 

Just use the index of the column, like so: 只需使用列的索引,如下所示:

In [3]: df = DataFrame([[0,1,2], [0,2,3], [1,2,3]])

In [4]: df
Out[4]:
   0  1  2
0  0  1  2
1  0  2  3
2  1  2  3

In [5]: df[0]
Out[5]:
0    0
1    0
2    1
Name: 0, dtype: int64

In [6]: df[df[0]==0]
Out[6]:
   0  1  2
0  0  1  2
1  0  2  3
import pandas as pd

df = pd.DataFrame({'a': [0, 0, 2, 1], 'b': [10, 5, 5, 2], 'c': [15, 16, 92, 2]})
zero_rows = df[df.iloc[:, 0] == 0]

To index by the column name instead: 要改为按列名索引:

zero_rows = df[df['a'] == 0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM