text_plain_output_1.png
import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

import seaborn as sns

df = pd.read_csv('/kaggle/input/thailand-fatal-road-accident/thailand_fatal_raod_accident_2011_2022.csv')

df['official_death_date'] = pd.to_datetime(df['official_death_date'])

thai_accident_df = df.dropna(subset='accident_date').copy()

thai_accident_df['accident_date'] = pd.to_datetime(thai_accident_df['accident_date'])
thai_accident_df.dtypes

thai_accident_df.describe().T

import matplotlib.pyplot as plt
import seaborn as sns
thai_accident_df = df.dropna(subset='accident_date').copy()
thai_accident_df['accident_date'] = pd.to_datetime(thai_accident_df['accident_date'])
thai_accident_df['year'], thai_accident_df['month'], thai_accident_df['day'] = (thai_accident_df['accident_date'].dt.year, thai_accident_df['accident_date'].dt.month, thai_accident_df['accident_date'].dt.day)
def thai_accident_from_to(from_date=thai_accident_df['accident_date'].min(), to_date=thai_accident_df['accident_date'].max()):
    df = thai_accident_df[(thai_accident_df['accident_date'] >= from_date) & (thai_accident_df['accident_date'] < to_date)]
    return df

counts = thai_accident_df.groupby(['year', 'month']).size().reset_index(name='count')

this_data = thai_accident_from_to("2019-01-01", "2021-05-31")

fig, ax = plt.subplots(2,2, figsize=(14,10))
fig.suptitle("Road Accident [2011-2022]")

# 00
sns.histplot(ax=ax[0,0], x=this_data["year"], discrete=True)
ax[0,0].set_title("Accident by year", y=1.05)
ax[0,0].bar_label(ax[0,0].containers[1])

# 01

this_data["province_en"].value_counts()[:10].sort_values().plot(kind="barh", ax=ax[0,1])
ax[0,1].set_title("Top 10 accident by province")
ax[0,1].bar_label(ax[0,1].containers[0], label_type="center", color="white")

# 10

this_data["vehicle_type"].value_counts().sort_values().plot(kind="barh", ax=ax[1,0])
ax[1,0].set_title("Top accident by vehicle type")
ax[1,0].bar_label(ax[1,0].containers[0])

# 11

ax[1,1] = sns.histplot(data=this_data, x="age",hue="gender", element="poly",discrete=True)
ax[1,1].set_title("Accident by age and gender")

plt.show()

gender_df = this_data['gender'].value_counts().reset_index()
plt.pie(gender_df['gender'], labels=gender_df['index'], autopct='%1.1f%%', explode=(0.1, 0, 0))
plt.show()
gender_df