【问题标题】:Finding index of a pandas DataFrame value查找 pandas DataFrame 值的索引
【发布时间】:2014-11-26 19:31:20
【问题描述】:

我正在尝试使用 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'

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

提前感谢您的帮助!

【问题讨论】:

  • 这个数据框只有 1 列还是你知道哪一列的最大值?如果您知道该列,那么 df.loc[df.col == max].index 将返回您的索引
  • 您好 EdChum,感谢您的回答。这样做会给我以下错误Traceback (most recent call last): File "psims2.py", line 81, in <module> print cd_gross_revenue.loc[cd_gross_revenue.col == max].index File "C:\Python27\lib\site-packages\pandas-0.14.1-py2.7-win32.egg\pandas\core\generic.py", line 18 43, in __getattr__ (type(self).__name__, name)) AttributeError: 'Series' object has no attribute 'col'
  • 我想你误会了,col 是您感兴趣的列的通用名称,所以用您的 df 中的列名替换列名,我的问题是这个 df 有多少列并且存在只有 1 或者您知道哪一列具有最大值,如果是,则使用该名称替换 col

标签: python python-2.7 pandas


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    s[s==13]

    例如,

    from pandas import Series
    
    s = Series(range(10,20))
    s[s==13]
    
    3    13
    dtype: int64
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2016-10-06
        • 2017-04-30
        • 2017-12-31
        • 2017-07-12
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-13
        相关资源
        最近更新 更多