Pandas:为数据帧中一列中的每个唯一单元格值筛选日期字段



我有一个数据帧df,如下

YearMonth    CustID    Values
201901       12231     400
201902       12231     233
201903       12231     244
201904       12231     355
201901       12235     114
201902       12235     133
201903       12235     144
201904       12235     205

在上文中,我有每个月的CustID及其相应值(Values((YearMonth(。

目标:

我想得到一个数据帧,其中每个客户端的数据将从201901(即2019年1月(到201903(即2019 3月(。因此得到的df看起来像

YearMonth    CustID    Values
201901       12231     400
201902       12231     233
201903       12231     244
201901       12235     114
201902       12235     133
201903       12235     144

我已经使用pd.to_datetime()YearMonth转换为datetime

如何获取上述数据帧?我应该先过滤,然后过滤groupby(['CustID'])吗?或者其他出路?

简单地说:

df = df[(df.YearMonth.ge(201901))&(df.YearMonth.le(201903))]

最新更新