简体   繁体   English

查找pandas DataFrame值的索引

[英]Finding index of a pandas DataFrame value

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. 我正在尝试使用熊猫来处理一些.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? maxindex变量使用idxmax()获得答案,但是如果我不查找最大值的索引怎么办? 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. 很明显.index在这里不适合我。

Thanks in advance for any help! 在此先感谢您的帮助!

Use a boolean mask to get the rows where the value is equal to the random variable. 使用boolean mask来获取值等于随机变量的行。 Then use that mask to index the dataframe or series. 然后使用该掩码索引数据帧或系列。 Then you would use the .index field of the pandas dataframe or series. 然后你将使用pandas数据帧或系列的.index字段。 An example is: 一个例子是:

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

s[s==13]

Eg, 例如,

from pandas import Series

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

3    13
dtype: int64

When you called idxmax it returned the key in the index which corresponded to the max value. 当你调用idxmax时,它返回索引中与最大值相对应的键。 You need to pass that key to the dataframe to get that value. 您需要将该密钥传递给数据帧才能获得该值。

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

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

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