python dataframe 取每行的最大值_Python Pandas Dataframe按组中的最大值选择行

I have a dataframe which was created via a df.pivot:

type start end

F_Type to_date

A 20150908143000 345 316

B 20150908140300 NaN 480

20150908140600 NaN 120

20150908143000 10743 8803

C 20150908140100 NaN 1715

20150908140200 NaN 1062

20150908141000 NaN 145

20150908141500 418 NaN

20150908141800 NaN 450

20150908142900 1973 1499

20150908143000 19522 16659

D 20150908143000 433 65

E 20150908143000 7290 7375

F 20150908143000 0 0

G 20150908143000 1796 340

I would like to filter and return a single row for each 'F_TYPE' only returning the row with the Maximum 'to_date'. I would like to return the following dataframe:

type start end

F_Type to_date

A 20150908143000 345 316

B 20150908143000 10743 8803

C 20150908143000 19522 16659

D 20150908143000 433 65

E 20150908143000 7290 7375

F 20150908143000 0 0

G 20150908143000 1796 340

Thanks..

解决方案

A standard approach is to use groupby(keys)[column].idxmax().

However, to select the desired rows using idxmax you need idxmax to return unique index values. One way to obtain a unique index is to call reset_index.

Once you obtain the index values from groupby(keys)[column].idxmax() you can then select the entire row using df.loc:

In [20]: df.loc[df.reset_index().groupby(['F_Type'])['to_date'].idxmax()]

Out[20]:

start end

F_Type to_date

A 20150908143000 345 316

B 20150908143000 10743 8803

C 20150908143000 19522 16659

D 20150908143000 433 65

E 20150908143000 7290 7375

F 20150908143000 0 0

G 20150908143000 1796 340

Note: idxmax returns index labels, not necessarily ordinals. After using reset_index the index labels happen to also be ordinals, but since idxmax is returning labels (not ordinals) it is better to always use idxmax in conjunction with df.loc, not df.iloc (as I originally did in this post.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值