Cid 2021-05-01| 2021-05-02 | 2021-05-03 | 2021-05-04| 2021-05-05|2021-05-06|2021-05-07|2021-05-08
120 25 30 40 10 15 20 5 5
220 10 20 30 10 50 15 35 55
430 16 4 20 10 25 25 15 6
In the requirement Cid is the index and columns are of int64 datatype
I want to convert the columnNames to datetime and resample them in groups of 7 based(i.e) weeks, starting from sunday.
I tried converting the columnNames to dateTime format but unable to do so for past 2 day Required Output
Cid 2021-05-01|2021-05-08
120 145 5
220 170 55
430 115 6
CodePudding user response:
You can use resample
to do 7 days aggregation.
# Convert columns to DatetimeIndex if it is not yet
df = df.set_index('Cid')
df.columns = pd.to_datetime(df.columns)
Then, aggregate the sum over 7 days. "W-SAT" is weekly interval ending on Saturday. You want the date from the left side of the interval, so add label and closed options to left.
df.resample('W-SAT', label='left', closed='left', axis=1).sum()
Result
2021-05-01 2021-05-08
Cid
120 145 5
220 170 55
430 115 6