多索引数据帧数据提取

时间:2020-08-21 14:24:52

标签: python python-3.x pandas

我有一个pandas数据框,第一行中有多个条目,而第二行中有重复的列。

                   A                           B.                     C
Date           High         Low            High       Low.        High        Low       
2000-07-03     19.796038    19.202157      9.261945   9.011265    19.261945   9.011265

2000-07-05     19.845534    18.845534      9.81945    8.011       29.81945    8.011

我想找出哪个A,B或C随日期的高低差异最大。

Desired output:
                     C
         Date        High          Low      Difference 
         2000-07-05  29.81945      8.011.   21.808

哪个更好,pivot_table,group_by,融化?

1 个答案:

答案 0 :(得分:3)

我们先做stack,然后再做idxmax

s=df.stack(level=0)
idx=(s['High']-s['Low']).idxmax()

df.loc[[idx[0]],[idx[1]]]
Out[268]: 
                   C       
                High    Low
Date                       
2000-07-05  29.81945  8.011
相关问题