如何在 Pandas Dataframe 中查找特定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43510589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:25:50  来源:igfitidea点击:

How to find Specific values in Pandas Dataframe

pythonpandasdataframe

提问by Shelly

I have imported the data in csv format in pandas. Can anybody tell me how i can find the values above 280 in one of the columns that i have and put them into another data frame. I have done the below code so far:

我已经将 csv 格式的数据导入到了Pandas中。谁能告诉我如何在我拥有的一列中找到 280 以上的值并将它们放入另一个数据框中。到目前为止,我已经完成了以下代码:

import numpy as np
import pandas as pd
df = pd.read_csv('...csv')

And the part of data is like the attached pic:enter image description here

部分数据如附图所示:在此处输入图片描述

回答by jezrael

You need boolean indexing:

你需要boolean indexing

df1 = df[df[2] > 280]

If need select also only column add loc:

如果需要也只选择列添加loc

s = df.loc[df[2] > 280, 2]

Sample:

样本:

df = pd.DataFrame({0:[1,2,3],
                   1:[4,5,6],
                   2:[107,800,300],
                   3:[1,3,5]})

print (df)
   0  1    2  3
0  1  4  107  1
1  2  5  800  3
2  3  6  300  5

df1 = df[df[2] > 280]
print (df1)
   0  1    2  3
1  2  5  800  3
2  3  6  300  5

s = df.loc[df[2] > 280, 2]
print (s)
1    800
2    300
Name: 2, dtype: int64

#one column df
df2 = df.loc[df[2] > 280, [2]]
print (df2)
     2
1  800
2  300