我还是python的新手,在尝试对一些数据进行分组以显示具有最高(最大)日期的记录时遇到问题,数据帧如下所示:
我正在尝试以下方法:
df_2 = df.max(axis = 0)
df_2 = df.periodo.max()
df_2 = df.loc[df.groupby('periodo').periodo.idxmax()]
它还给了我:
Timestamp('2020-06-01 00:00:00')
periodo 2020-06-01 00:00:00
valor 3.49136
虽然'periodo'的值是正确的,但'valor'的值不是正确的,因为我需要获得相应的完整记录('period'和'value'),而不是每个记录的最大值。我尝试过其他方法,但我无法得到我想要的
我需要做什么
提前谢谢你,我会留意你的回答
问候
创建示例数据帧
ie
df
将是:选择每列中条目最高的行
你可以做:
(或者,如果想使用
idxmax
,可以这样做:df.loc[[df['dates'].idxmax()]]
)返回:
即这是最新日期的行
&
(或者,如果想再次使用
idxmax
,可以这样做:df.loc[[df['values'].idxmax()]]
-就像Scott Boston的answer。)及
即这是
values
列中具有最高值的行Reference
我想你需要这样的东西:
在“valor”列上使用
idxmax
。然后使用该索引选择该行MVCE:
输出:
相关问题 更多 >
编程相关推荐