桑基图
程序员文章站
2022-05-25 21:17:40
...
import pandas as pd
from pyecharts.charts import Sankey
from pyecharts import options as opts
df = pd.read_excel('D:/Studyfile/test.xlsx')
df.head(10)
nodes = []
for i in range(3):
values = df.iloc[:,i].unique()
for value in values:
dic = {}
dic['name'] = value
nodes.append(dic)
nodes
first = df.groupby(['品类','第一次购买'])['人数'].sum().reset_index()
second = df.iloc[:,1:]
first.columns = ['source','target','value']
second.columns = ['source','target','value']
result = pd.concat([first,second])
result.head(10)
linkes = []
for i in result.values:
dic = {}
dic['source'] = i[0]
dic['target'] = i[1]
dic['value'] = i[2]
linkes.append(dic)
linkes
pic = (
Sankey()
.add('',
nodes,
linkes,
linestyle_opt=opts.LineStyleOpts(opacity = 0.7,curve = 0.5,color = 'source'),
label_opts = opts.LabelOpts(position = 'top'),
node_gap = 30,
)
.set_global_opts(title_opts=opts.TitleOpts(title = '客户购买路径流转图'))
)
pic.render('D:/Studyfile/test2.html')
下一篇: 学习笔记21.07.13:绘制桑基图