1

我有以下 DataFrame,并且喜欢按月分组。

import pandas as pd
import numpy as np

idx = pd.date_range(start='2001-01-01', end='2002-01-01', periods = 80)
df = pd.DataFrame(np.random.rand(160).reshape(80,2), index=idx.normalize(), columns=['a','b'])

使用以下代码,我可以df按月分组,但它的索引标签是每个日历月的最后几天,而不是df.

k = df.resample('M').apply(lambda x: x[-1])

k1 = df.groupby(pd.Grouper(freq='M')).last()

例如,df.loc['2001-01'].index[-1]Timestamp('2001-01-28 00:00:00'),但不是Timestamp('2001-01-31 00:00:00')。但是,k包括k1如下2001-01-31

                   a         b
2001-01-31  0.521604  0.716046
2001-02-28  0.584479  0.560608
2001-03-31  0.201605  0.860491
2001-04-30  0.077426  0.711042
2001-05-31  0.544708  0.865880
2001-06-30  0.755516  0.863443
2001-07-31  0.266727  0.107859
2001-08-31  0.683754  0.098337
2001-09-30  0.586217  0.697163
2001-10-31  0.742394  0.160754
2001-11-30  0.655662  0.400128
2001-12-31  0.902192  0.580582
2002-01-31  0.878815  0.555669

换句话说,我喜欢按月分组,df并且分组df有每个月最后几天的索引标签df,而不是每个日历月的最后日期。

4

2 回答 2

1

duplicated让我们在修剪索引后尝试

df = df.sort_index()
out = df[~df.index.strftime('%Y-%m').duplicated(keep='last')]
Out[242]: 
                   a         b
2001-01-28  0.984408  0.923390
2001-02-25  0.108587  0.797240
2001-03-29  0.058016  0.025948
2001-04-26  0.095034  0.226460
2001-05-28  0.386954  0.419999
2001-06-30  0.535202  0.576777
2001-07-27  0.389711  0.706282
2001-08-29  0.270434  0.342087
2001-09-30  0.190336  0.872519
2001-10-28  0.333673  0.832585
2001-11-29  0.651579  0.751776
2001-12-27  0.649476  0.748410
2002-01-01  0.670143  0.389339
于 2022-02-21T14:00:25.247 回答
1

@BENY 的回答非常适合这个问题。但是,如果您需要对组做更多的事情(例如,计算一些聚合统计数据),那么这里有另一个使用该groupby方法的想法:

df = df.reset_index()
df.groupby([(df["index"].dt.year),(df["index"].dt.month)]).last().set_index("index")
于 2022-02-21T14:14:26.093 回答