gpt4 book ai didi

python , Pandas : return highest values from multiindex

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:35 24 4
gpt4 key购买 nike

让我们考虑一个 pandas DataFrame 定义如下:

from decimal import Decimal
from pandas import Timestamp
dic={'volume': {('CSC', Timestamp('2016-08-05 00:00:00'), 'CSCF7'): Decimal('13'),
('CSC', Timestamp('2016-08-05 00:00:00'), 'CSCG7'): Decimal('6'),
('CSC', Timestamp('2016-08-05 00:00:00'), 'CSCH7'): Decimal('12'),
('DA', Timestamp('2016-08-05 00:00:00'), 'DCF7'): Decimal('47'),
('DA', Timestamp('2016-08-05 00:00:00'), 'DCG7'): Decimal('16'),
('DA', Timestamp('2016-08-05 00:00:00'), 'DCH7'): Decimal('27')
}}

df=pd.DataFrame(dic)

enter image description here

我想对其进行转换,以便它返回第三个索引级别的最高值。例如,在当前示例中,将是:

                    highest  
CSC 2016-08-05 CSCF7
DA 2016-08-05 DCF7

有人知道如何执行此操作吗?

最佳答案

您可以在level上进行groupby并获取idxmax

In [317]: df.groupby(level=0).idxmax()
Out[317]:
volume
CSC (CSC, 2016-08-05 00:00:00, CSCF7)
DA (DA, 2016-08-05 00:00:00, DCF7)

In [318]: df.groupby(level=0).idxmax().volume.apply(pd.Series)
Out[318]:
0 1 2
CSC CSC 2016-08-05 CSCF7
DA DA 2016-08-05 DCF7

或者,

In [338]: df.groupby(level=[0, 1]).volume.idxmax().apply(lambda x: x[-1])
Out[338]:
CSC 2016-08-05 CSCF7
DA 2016-08-05 DCF7
Name: volume, dtype: object

或者,

In [341]: df.groupby(level=[0, 1]).volume.idxmax().str[-1]
Out[341]:
CSC 2016-08-05 CSCF7
DA 2016-08-05 DCF7
Name: volume, dtype: object

关于 python , Pandas : return highest values from multiindex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com