大家好,我是欧K~
本期我们利用Python分析「交通事故数据集」,看看:交通事故时间分布、事故车辆颜色分布、最多交通事故起因、各事故责任划分占比、驾驶员性别占比、驾驶员驾龄占比、事故车辆品牌分布等等,希望对大家有所帮助,如有疑问或者需要改进的地方可以联系小编。
涉及到的库:
Pandas — 数据处理
Pyecharts — 数据可视化
import pandas as pd
from pyecharts.charts import *
from pyecharts import options as opts
import warnings
warnings.filterwarnings('ignore')
2.1 读取数据
df = pd.read_excel('./交通事故数据集.xlsx')
2.2 数据信息
df.info()
2.3 去除空数据
df = df.dropna()
df = df.drop_duplicates()
df = df[~(df['责任划分'] == '不负责任')]
df['事故时间_年'] = df['时间_td'].dt.year
df['事故时间_月'] = df['时间_td'].dt.month
df['事故时间_日'] = df['时间_td'].dt.day
df['事故时间_时'] = df['时间_td'].dt.hour
df1['驾驶员出生日期_年'] = df1['驾驶员出生日期_年'].astype('int')
df1['驾驶员年龄'] = df1['事故时间_年'] - df1['驾驶员出生日期_年']
def get_line1():
chart = (
Line()
.add_xaxis(x_data)
.add_yaxis('', y_data)
.set_global_opts(
title_opts=opts.TitleOpts(
title='1-交通事故时间分布',
subtitle=subtitle,
pos_top='2%',
pos_left='center',
title_textstyle_opts=opts.TextStyleOpts(color=lab_color, font_size=20)
),
visualmap_opts=opts.VisualMapOpts(
is_show=False,
range_color=range_color,
),
) )
一个月的数据中每天发生交通事故的数量差异不是很大。
3.3 交通事故小时分布
def get_line3():
chart = (
Line()
.add_xaxis(x_data)
.add_yaxis('', y_data)
.set_global_opts(
title_opts=opts.TitleOpts(
title='3-交通事故小时分布',
subtitle=subtitle,
pos_top='2%',
pos_left='center',
title_textstyle_opts=opts.TextStyleOpts(color=lab_color, font_size=20)
),
visualmap_opts=opts.VisualMapOpts(
is_show=False,
range_color=range_color,
),
)
)
上图可以看出:交通时间基本集中在早上8时-下午20时,原因应该是白天出行人流量比较大,事故发生率比较高。
def get_bar():
chart = (
Bar()
.add_xaxis(x_data)
.add_yaxis('', y_data,label_opts=opts.LabelOpts(position='right'))
.set_global_opts(
title_opts=opts.TitleOpts(
title='4-交通事故车辆颜色TOP10',
subtitle=subtitle,
pos_top='2%',
pos_left='center',
title_textstyle_opts=opts.TextStyleOpts(color=lab_color, font_size=20)
),
legend_opts=opts.LegendOpts(is_show=False)
)
)
交通事故车辆颜色:白色(9364起)、黑色(3098起)、银色(2068起)、红色(1564起)、蓝色(1022起)
3.5 交通事故起因
交通事故原因最多的是未按规定让行的事故(7188起),其次是追尾的(5153起),以及倒车的(1611起)等等。
def get_bar():
chart = (
Pie()
.add('',
datas,
center=['50%', '55%'],
label_opts=opts.LabelOpts(formatter='{b}: {c} ({d}%)')
)
.set_global_opts(
title_opts=opts.TitleOpts(
title='6-责任划分占比',
subtitle=subtitle,
pos_top='2%',
pos_left='center',
title_textstyle_opts=opts.TextStyleOpts(color=lab_color, font_size=20)
),
) )
负全部责任事故约占比:91%(16765起),负同等责任事故约占比:9%(1665起)。
def get_scatter1():
scatter1 = (
Scatter()
.add_xaxis(x_data)
.add_yaxis('', y_data,
)
.set_global_opts(
title_opts=opts.TitleOpts(
title='7-负利润行业数量',
subtitle=subtitle,
pos_top='2%',
pos_left="center",
),
visualmap_opts=opts.VisualMapOpts(
type_='size',
),
)
)
男性事故约占比:93%(17149起),女性事故约占比:7%(1281起)。
5-15年驾龄区间出现事故的数量较其他驾龄区间高,1年以下驾龄的新手出现事故的数量是最低的。
def get_wordcloud(x_data,y_data):
wordcloud = (
WordCloud()
.add(series_name="", data_pair, word_size_range=[5, 45])
.set_global_opts(
title_opts=opts.TitleOpts(
title='10-车辆品牌词云',
subtitle=subtitle,
pos_top='1%',
pos_left="1%",
)
)
)
交通事故最多的10大汽车品牌:长安、五菱、大众汽车、丰田、雪佛兰、别克、解放、福克斯、北京现代、东风标致
👉 在线运行:
以上就是本期为大家整理的全部内容了,喜欢的朋友可以点赞、点在看也可以分享让更多人知道。
往期推荐
源码下载 | 【01-50】Pthon可视化系列文章资源(源码+数据)
可视化 | Flask+Mysql+Echarts 豆瓣电影Top250数据分析系统
56 | 2024年中国500强企业数据分析可视化
53 | 基于Lasso回归和随机森林的上海链家二手房房价预测
52 | 基于KNN近邻和随机森林模型对用户转化进行分析与预测
Pandas+Pyecharts | 全国热门旅游景点数据分析可视化
可视化 | 分享一套Flask+Pyecharts可视化模板
用Python分析了3W+《独行月球》影评数据,看看观众们怎么说~