查找 Pandas DataFrame 值的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26150171/
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-13 22:32:12  来源:igfitidea点击:

Finding index of a pandas DataFrame value

pythonpython-2.7pandas

提问by ploo

I am trying to process some .csv data using pandas, and I am struggling with something that I am sure is a rookie move, but after spending a lot of time trying to make this work, I need your help.

我正在尝试使用 Pandas 处理一些 .csv 数据,并且我正在努力解决一些我确定是新手的举动,但是在花了很多时间尝试完成这项工作之后,我需要您的帮助。

Essentially, I am trying to find the index of a value within a dataframe I have created.

本质上,我试图在我创建的数据框中找到值的索引。

max = cd_gross_revenue.max()
#max value of the cd_gross_revenue dataframe

print max
#finds max value, no problem!

maxindex = cd_gross_revenue.idxmax()
print maxindex
#finds index of max_value, what I wanted!

print max.index
#ERROR: AttributeError: 'numpy.float64' object has no attribute 'index'

The maxindex variable gets me the answer using idxmax(), but what if I am not looking for the index of a max value? What if it is some random value's index that I am looking at, how would I go about it? Clearly .index does not work for me here.

maxindex 变量使用 idxmax() 为我提供了答案,但是如果我不寻找最大值的索引怎么办?如果我正在查看某个随机值的索引,该怎么办?显然 .index 在这里对我不起作用。

Thanks in advance for any help!

在此先感谢您的帮助!

采纳答案by Daniel Velkov

Use a boolean maskto get the rows where the value is equal to the random variable. Then use that mask to index the dataframe or series. Then you would use the .indexfield of the pandas dataframe or series. An example is:

使用 aboolean mask获取值等于随机变量的行。然后使用该掩码来索引数据框或系列。然后您将使用.indexPandas数据框或系列的字段。一个例子是:

In [9]: s = pd.Series(range(10,20))

In [10]: s
Out[10]:

0    10
1    11
2    12
3    13
4    14
5    15
6    16
7    17
8    18
9    19
dtype: int64

In [11]: val_mask = s == 13

In [12]: val_mask

Out[12]:
0    False
1    False
2    False
3     True
4    False
5    False
6    False
7    False
8    False
9    False
dtype: bool

In [15]: s[val_mask]
Out[15]:
3    13
dtype: int64

In [16]: s[val_mask].index
Out[16]: Int64Index([3], dtype='int64')

回答by Adam Hughes

s[s==13]

s[s==13]

Eg,

例如,

from pandas import Series

s = Series(range(10,20))
s[s==13]

3    13
dtype: int64

回答by b10n

When you called idxmax it returned the key in the index which corresponded to the max value. You need to pass that key to the dataframe to get that value.

当您调用 idxmax 时,它返回索引中对应于最大值的键。您需要将该键传递给数据框以获取该值。

max_key = cd_gross_revenue.idxmax()
max_value = cd_gross_revenue.loc[max_key]