记录在可视化的常用方法
读取数据
import pandas as pd
#pandas读取数据
excel_data = pd.read_excel("D:\\py_taidi\\keshihua\\可视化数据.xlsx")
删除 death_data 列为空的行
notnull = excel_data.dropna(subset=["death_data"])
使用 as_index=False 将 as_index设置为 False后 分组列将不会被作为索引
city_shanghai_dzsw_count = city_shanghai_dzsw[['com_addr', 'se_cat']].groupby(by=['se_cat'], as_index=False).agg('count')
se_cat com_addr
0 3C电子 6
1 二手电商 2
2 其他电商服务 8
3 化妆品 3
4 大宗商品 1
5 奢侈品 4
6 家居家纺 3
7 数字虚拟商品 1
8 服装服饰 24
9 母婴电商 5
10 珠宝首饰 3
11 生鲜食品 15
12 电商解决方案 17
13 社区电商 1
14 综合电商 35
15 跨境电商 11
不使用 as_index=False 分组列 se_cat将被作为索引
com_addr
se_cat
3C电子 6
二手电商 2
其他电商服务 8
化妆品 3
大宗商品 1
奢侈品 4
家居家纺 3
数字虚拟商品 1
服装服饰 24
母婴电商 5
珠宝首饰 3
生鲜食品 15
电商解决方案 17
社区电商 1
综合电商 35
跨境电商 11
根据 death_data 分组进行count统计
date = notnull[['com_name', 'death_data']].groupby(by='death_data').agg('count')
com_name
death_data
1985-08-08 1
1990-12-31 1
2000-08-11 1
2003-07-31 1
2003-08-03 1
...
2019-10-17 1
2019-10-29 1
2019-11-01 1
时间类型转换
# 将数据类型转换为日期类型
t['date'] = pd.to_datetime(t['death_data'])
将时间 date 作为索引
# 将date设置为index
df = t.set_index('date')
bianh com_name com_addr cat se_cat death_data
date
2019-11-25 1 麦子金服 上海 金融 借贷 2019-11-25
2019-11-24 2 拓道金服 浙江 金融 借贷 2019-11-24
2019-11-01 3 云柏科技 广东 医疗健康 医疗器械及硬件 2019-11-01
2019-10-29 4 迷你生鲜 福建 电子商务 生鲜食品 2019-10-29
2019-10-17 5 一生健康 北京 医疗健康 寻医诊疗 2019-10-17
... ... ... ... ... ...
2003-08-03 6268 小课桌 江苏 教育 K12 2003-08-03
2003-07-31 6269 旅行者 上海 旅游 旅游综合服务 2003-07-31
2000-08-11 6270 3C物联网 上海 企业服务 IT基础设施 2000-08-11
1990-12-31 6271 广信房产 广东 金融 投融资 1990-12-31
1985-08-08 6272 德赛信息科技 广东 硬件 智能家居 1985-08-08
根据 date 日期的年份进行分组统计
count = t['date'].groupby(t['date'].dt.year).agg('count')
print(count.index)
打印出的索引
Int64Index([1985, 1990, 2000, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019],
dtype='int64', name='date')
pyecharts画图
from pyecharts import options as opts
from pyecharts.charts import Bar
bar = (
Bar(init_opts=opts.InitOpts(width='1500px', height='400px'))
.add_xaxis(count.index.tolist())
.add_yaxis("各年份被淘汰的公司总数对比", count.values.tolist())
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-各年份被淘汰的公司总数对比"))
# .set_colors("red") # 柱子的颜色
.set_global_opts(
# x轴
xaxis_opts=opts.AxisOpts(
name='年份',
name_location='middle',
name_gap=20,
# x轴名称的格式配置
name_textstyle_opts=opts.TextStyleOpts(
font_family='Times New Roman',
font_size=14,
),
),
# y轴
yaxis_opts=opts.AxisOpts(
name='年份淘汰总数',
name_location='middle',
name_gap=30,
name_textstyle_opts=opts.TextStyleOpts(
font_family='Times New Roman',
font_size=14,
color='black',
),
axistick_opts=opts.AxisTickOpts(
# is_show=False, # 是否显示
is_inside=True, # 刻度线是否在内侧
),
axislabel_opts=opts.LabelOpts(
font_size=12,
font_family='Times New Roman',
),
),
# 设置标题
title_opts=opts.TitleOpts(
title='各年份淘汰总数',
)
)
)
bar.render()
排序:使用 .sort_values(by=‘sort’,ascending=False) 默认为升序,ascending=False为降序
city_beijing_count = city_beijing[['com_addr','cat']].groupby(by=['cat'],as_index=False).agg('count')
city_beijing_count['sort'] = city_beijing_count.values[:,1:]
print(city_beijing_count.sort_values(by='sort',ascending=False))