查找大熊猫DataFrame值的索引

plo*_*loo 5 python python-2.7 pandas

我正在尝试使用熊猫处理一些.csv数据,但我正在努力确定自己是菜鸟,但在花了很多时间尝试使这项工作之后,我需要您的帮助。

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

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'
Run Code Online (Sandbox Code Playgroud)

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

在此先感谢您的帮助!

Dan*_*kov 4

使用 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')
Run Code Online (Sandbox Code Playgroud)