【问题标题】:Pandas DataFrame Retrieve Value from ColumnPandas DataFrame 从列中检索值
【发布时间】:2013-09-22 23:30:12
【问题描述】:

假设我有如下数据框:

Idx A B C
0   1 2 3
1   3 4 5
2   2 3 8

数据框B列的最大值为4,我可以从df["B"].argmax()得到索引,即1。

现在的问题是,我怎样才能得到数据框列 B 的确切最大值?

谢谢!

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:
    In [6]: df
    Out[6]: 
         A  B  C
    Idx         
    0    1  2  3
    1    3  4  5
    2    2  3  8
    
    In [7]: df.max()
    Out[7]: 
    A    3
    B    4
    C    8
    dtype: int64
    
    In [10]: df['B'].max()
    Out[10]: 4
    
    In [8]: df.idxmax()
    Out[8]: 
    A    1
    B    1
    C    2
    dtype: int64
    

    【讨论】:

    • 是的。现在,我有来自 df.idxmax() 的索引。如何使用该索引来获取 A 列的值?我尝试过类似 df["A"](df.idxmax()) 的方法,但它不起作用?谢谢!
    • df['A'].max()df['A'](df.idxmax()['A']) 更冗长
    猜你喜欢
    • 2021-01-29
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多