Issue
How to judge whether the date cell is within the range from 08:00 of the previous day (excluded) to 08:00 of the cell day (included) according to the date cell time, and calculate the sum of data in the corresponding range.
original data:
date | data |
---|---|
2022-02-01 07:00 | 1 |
2022-02-01 09:00 | 2 |
2022-02-01 14:00 | 1 |
2022-02-01 23:00 | 3 |
2022-02-02 09:00 | 1 |
2022-02-03 10:00 | 2 |
2022-02-04 11:50 | 3 |
result data:
date | data |
---|---|
2022-02-01 08:00 | 1 |
2022-02-02 08:00 | 6 |
2022-02-03 08:00 | 1 |
2022-02-04 08:00 | 2 |
2022-02-05 08:00 | 3 |
Solution
import pandas as pd
df = pd.read_clipboard()
df1 = df.astype({'date':'datetime64[ns]'})\
.resample('D',offset='8h',label='right',
closed='right',on='date')\
.sum()
date | data |
---|---|
2022-02-01 08:00:00 | 1 |
2022-02-02 08:00:00 | 6 |
2022-02-03 08:00:00 | 1 |
2022-02-04 08:00:00 | 2 |
2022-02-05 08:00:00 | 3 |
Answered By - z-ali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.