python - In pandas how to filter based on a particular weekday and range of time -
my data frame looks this.the notebook here
c/a unit scp daten timen descn entriesn exitsn 0 a002 r051 02-00-00 08-18-12 00:00:00 regular 3759779 1297676 1 a002 r051 02-00-00 08-18-12 04:00:00 regular 3759809 1297680 2 a002 r051 02-00-00 08-18-12 08:00:00 regular 3759820 1297701 3 a002 r051 02-00-00 08-18-12 12:00:00 regular 3759879 1297799 4 a002 r051 02-00-00 08-18-12 16:00:00 regular 3760073 1297863 5 a002 r051 02-00-00 08-18-12 20:00:00 regular 3760367 1297920 6 a002 r051 02-00-00 08-19-12 00:00:00 regular 3760494 1297958 7 a002 r051 02-00-00 08-19-12 04:00:00 regular 3760525 1297962 8 a002 r051 02-00-00 08-19-12 08:00:00 regular 3760545 1297983 9 a002 r051 02-00-00 08-19-12 12:00:00 regular 3760603 1298048 10 a002 r051 02-00-00 08-19-12 16:00:00 regular 3760750 1298104 11 a002 r051 02-00-00 08-19-12 20:00:00 regular 3760982 1298137 12 a002 r051 02-00-00 08-20-12 00:00:00 regular 3761088 1298175 13 a002 r051 02-00-00 08-20-12 04:00:00 regular 3761098 1298186 14 a002 r051 02-00-00 08-20-12 08:00:00 regular 3761130 1298265
this code filter out month of july
july_station = df[['countn']]\ [(df.datetimen >= datetime.datetime.strptime('07-01-13', '%m-%d-%y')) &\ (df.datetimen <= datetime.datetime.strptime('07-31-13', '%m-%d-%y'))]\ .groupby(df.unit)\ .sum()
the above code filters month
what if have filter out entries between midnight & 4am on fridays in july 2013? right approach?
july_station1 = df[['countn']]\ [(df.datetimen >= datetime.datetime.strptime('07-01-13 00:00 5', '%m-%d-%y %h:%m %a')) &\ (df.datetimen <= datetime.datetime.strptime('07-31-13 04:00 5', '%m-%d-%y %h:%m %a'))]\ .groupby(df.unit)\ .sum()
if column datetime column, can weekday , hour column.dt.weekday
(monday = 0, sunday = 6), , column.dt.hour
. can use between
on series range comparison more elegantly:
df.daten = pd.to_datetime(df.daten) df.timen = pd.to_datetime(df.timen) mask = (df.daten == 4) & df.timen.dt.hour.between(0,4)
Comments
Post a Comment