python - Pandas date ranges and averaging the counts -
i have below pandas dataframe
stdate enddate count 2004-01-04 2004-01-10 68 2004-01-11 2004-01-17 100 2004-01-18 2004-01-24 83 2004-01-25 2004-01-31 56 2004-02-01 2004-02-07 56 2004-02-08 2004-02-14 68 2004-02-15 2004-02-21 81 2004-02-22 2004-02-28 68 2004-02-29 2004-03-06 76
i want take average of count based on month:
that wanted like:
date count 2004-01 (306/25-4) 2004-02 (349/28-01)
for example second month enddate 3, (i need in aggregarting counts using pandas)
it's not complicated, there bit of work, , think should ditch pandas
of calculation, , build dataframe right @ end.
suppose have 2 datetime
objects, b
, e
. difference between them in days is
(e - b).days
this gives how count of row divided days.
also, given month, can find last day of month using calendar
module.
so, following:
counts_per_month = {} def process_row(b, e, count): ... # find how count splits between months, # update counts_per_month accordingly
now call
df.apply(lambda r: process_row(r.stdate, r.enddate, r.count), axis=1)
at point counts_per_month
contain data. finish off calling pd.dataframe.from_dict
.
Comments
Post a Comment