简体   繁体   中英

Pandas: Filter datefield for each unique cell values in a column in dataframe

I have a dataframe df as follows

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

In the above, I have CustID and its corresponding values ( Values ) for each month ( YearMonth ).

Objective:

I would like to get a dataframe where for each client data will be till 201903 ie March 2019 from 201901 ie January 2019. So the resultant df looks like

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

I have converted YearMonth to datetime using pd.to_datetime() .

How to get the above dataframe? Should I filter first and then groupby(['CustID']) ? Or any other way out?

what about simply:

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

using pd.to_datetime with dt.month accessor and series.between() :

df[pd.to_datetime(df.YearMonth,format='%Y%m').dt.month.between(1,3)]

   YearMonth  CustID  Values
0     201901   12231     400
1     201902   12231     233
2     201903   12231     244
4     201901   12235     114
5     201902   12235     133
6     201903   12235     144

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM