Skip to content

Instantly share code, notes, and snippets.

@gynvael
Created June 4, 2023 06:37
Show Gist options
  • Save gynvael/462da1034c5830ecca2e04490ba0e84b to your computer and use it in GitHub Desktop.
Save gynvael/462da1034c5830ecca2e04490ba0e84b to your computer and use it in GitHub Desktop.
Park places chart
"""
GPT4
I have a CSV file with two fields: data and time, and parking space occupancy. Example:
2019-05-18T02:20:08,162
2019-05-18T01:55:38,160
2019-05-18T02:00:02,160
2019-05-18T02:05:37,161
2019-05-18T02:35:37,162
2019-05-18T02:50:34,160
Create a Python script which reads this CSV files, then groups them by week day (e.g. Monday, Tuesday, etc), and then calculates the average for each hour on each week day.
Then generate a plot (table) where the vertical axis is the hour in day, the horizontal is the day of the week, and each cell denotes with a color and number how many places are occupied on that day of the week at that hour on average.
"""
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from datetime import datetime
# Load the CSV data into a DataFrame
df = pd.read_csv('file.csv', names=['datetime', 'occupancy'])
# Convert the 'datetime' column to a datetime object
df['datetime'] = pd.to_datetime(df['datetime'])
# Extract the day of the week and hour from the datetime
df['weekday'] = df['datetime'].dt.day_name()
df['hour'] = df['datetime'].dt.hour
# Set the 'weekday' column to an ordered category so that the days of the week appear in the order we expect
df['weekday'] = pd.Categorical(df['weekday'], categories=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], ordered=True)
# Group the data by weekday and hour, calculate the average occupancy
grouped = df.groupby(['weekday', 'hour']).mean().reset_index()
# Create a pivot table for the heatmap
pivot_table = grouped.pivot('weekday', 'hour', 'occupancy')
# Generate the heatmap
plt.figure(figsize=(10, 7))
sns.heatmap(pivot_table, annot=True, fmt=".1f", cmap="YlGnBu")
plt.title("Average Parking Space Occupancy")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment