Home > Net >  How do i filter by specific event for my graph plotting
How do i filter by specific event for my graph plotting

Time:11-10

I have a CSV File with columns below:

Time Event Speed
1/30/2022 17:23 Speeding 50
1/28/2022 18:22 Speeding 20
1/27/2022 22:00 Speeding 30
1/26/2022 23:23 Speeding 40
1/27/2022 22:00 Stopping 10
1/26/2022 23:23 Stopping 10

Issue: Currently my code will run and give me the average speed of every event and plot it against the time in 24hr format. With x-axis being the time and y-axis being the average speed. However i am only trying to get average speed of the Event, "Speeding" from this csv example template. Instead of every event's average speed.

I tried filtering this way:

Tried Event Filtering codes

event_filter = df["Event"] == "Speeding"
dr = df[event_filter]
grouped_by_event1 = dr.groupby('Event')[['Speed']]

Current Codes (It plots average speed of all event against 24hr time):

import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, HoverTool

output_file('Speed.html')  # output for average speed graph

file = 'C:/Users/oof/Desktop/route.csv' 
df = pd.read_csv(file)  

df['Time'] = pd.to_datetime(df['Time'], format='%m/%d/%Y %H:%M').dt.time
grouped_by_time = df.groupby('Time')[['Speed']].mean()
print(grouped_by_time)

source1 = ColumnDataSource(grouped_by_time)
p1 = figure(x_axis_type='datetime')
p1.line(x='Time', y='Speed', line_width=2, source=source1)
p1.title.text = 'Average Speed in a Day'
p1.yaxis.axis_label = 'Average Speed'
p1.xaxis.axis_label = '24-Hour'

p1.xaxis.major_label_overrides = {0: '0h', 24*60*60*1000: '24h'}
TOOLTIP1 = [("Time", "@Time{%H:%M}"),
            ("Average Speed", "@Speed")]
p1.add_tools(HoverTool(tooltips=TOOLTIP1, formatters={"@Time": "datetime"}, mode='vline'))

show(p1)

CodePudding user response:

Filter by Speeding then group by Time:

event_filter = df["Event"] == "Speeding"
grouped_by_time = df[event_filter].groupby('Time')[['Speed']].mean()

And it plots average speed of Speeding events (50, 20, 30, 40): enter image description here

The whole code as below:

import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, HoverTool

output_file('Speed.html')  # output for average speed graph

file = 'D:/test/TestPython/route.csv' 
df = pd.read_csv(file)  

df['Time'] = pd.to_datetime(df['Time'], format='%m/%d/%Y %H:%M').dt.time

# NOTE: filter by Speeding, then group by Time
event_filter = df["Event"] == "Speeding"
grouped_by_time = df[event_filter].groupby('Time')[['Speed']].mean()
print(grouped_by_time)

source1 = ColumnDataSource(grouped_by_time)
p1 = figure(x_axis_type='datetime')
p1.line(x='Time', y='Speed', line_width=2, source=source1)
p1.title.text = 'Average Speed in a Day'
p1.yaxis.axis_label = 'Average Speed'
p1.xaxis.axis_label = '24-Hour'

p1.xaxis.major_label_overrides = {0: '0h', 24*60*60*1000: '24h'}
TOOLTIP1 = [("Time", "@Time{%H:%M}"),
            ("Average Speed", "@Speed")]
p1.add_tools(HoverTool(tooltips=TOOLTIP1, formatters={"@Time": "datetime"}, mode='vline'))

show(p1)

  • Related