注意:仅当每个组至少有3行时,此解决方案才有效
请尝试以下方法:
In [59]: x = (df.groupby(pd.Grouper(freq='H'))['VAL'] .apply(lambda x: x.nlargest(3)) .reset_index(level=1, drop=True) .to_frame('VAL'))In [60]: xOut[60]: VALTIME2017-12-08 00:00:00 822017-12-08 00:00:00 562017-12-08 00:00:00 532017-12-08 01:00:00 952017-12-08 01:00:00 872017-12-08 01:00:00 792017-12-08 02:00:00 882017-12-08 02:00:00 782017-12-08 02:00:00 41In [61]: x.set_index(np.arange(len(x)) % 3, append=True)['VAL'].unstack().add_prefix('VAL')Out[61]: VAL0 VAL1 VAL2TIME2017-12-08 00:00:00 82 56 532017-12-08 01:00:00 95 87 792017-12-08 02:00:00 88 78 41一些解释:
In [94]: x.set_index(np.arange(len(x)) % 3, append=True)Out[94]: VALTIME2017-12-08 00:00:00 0 82 1 56 2 532017-12-08 01:00:00 0 95 1 87 2 792017-12-08 02:00:00 0 88 1 78 2 41In [95]: x.set_index(np.arange(len(x)) % 3, append=True)['VAL'].unstack()Out[95]:0 1 2TIME2017-12-08 00:00:00 82 56 532017-12-08 01:00:00 95 87 792017-12-08 02:00:00 88 78 41



