1. 程式人生 > 其它 >Python資料分析+視覺化專案教學:分析猛男童年的玩具,並可視化展示商品資料

Python資料分析+視覺化專案教學:分析猛男童年的玩具,並可視化展示商品資料

前言

你相信光嗎(那年要不是我拿著手電筒照著電視機,迪迦奧特曼早就被打到了)

來自京東平臺上的資料,萬代奧特曼與萬代高達以及樂高三大型別玩具的資料對比分析,消費者更愛哪一類?

那麼,今天我們來分析一下,猛男的童年回憶:高達、樂高、奧特曼

Python從零基礎入門到實戰系統教程、原始碼、視訊,想要資料集的同學也可以點這裡

採集資料部分我就不再講了,想了解的可以看《京東電商平臺商品資料爬取》,這次的資料也是在京東平臺上的資料

開始程式碼部分

一次性匯入所需要的全部第三方庫

import pandas as pd 
import pyecharts.options as opts
from pyecharts.charts import * from pyecharts.globals import ThemeType#設定主題 from pyecharts.commons.utils import JsCode

1. 讀取資料,而這些資料,一般都是我們爬取到的商品資料,或者公司內的資料庫裡面的資料

df1 = pd.read_csv(r'京東-樂高.csv', engine='python', encoding='utf-8-sig')
df2 = pd.read_csv(r'6K高達.csv', engine='python', encoding='utf-8-sig
') df3 = pd.read_csv(r'6K奧特曼.csv', engine='python', encoding='utf-8-sig')

檢視下資料

df1.head(1)

2. 資料處理

把表格統計到一起

df_all = pd.concat([df1,df2,df3])
df_all.info()

除去重複值

df_all.drop_duplicates(inplace=True)

刪除不必要的列

df_all = df_all.drop(['商品SKU','商品連結','封面圖連結','評論連結','店鋪連結
','頁碼','當前時間','頁面網址'],axis=1) df_all.head(1)

篩選剔除廣告

df_all = df_all[df_all['是否廣告'] == '']

重置索引

df_all = df_all.reset_index(drop=True)
df_all.info()

3. 處理完資料以後,我們就可以做視覺化圖表了

繪製商家上線的商品數目Top20柱狀圖
bar1 = (
    Bar(init_opts=opts.InitOpts(theme='dark', width='1000px',height ='500px'))
    .add_xaxis(shopname.index.tolist())
    .add_yaxis("",shopname.values.tolist())
    .set_series_opts(
        label_opts=opts.LabelOpts(
                is_show=True, 
                position='insideRight',
                font_style='italic'
            ),
        itemstyle_opts=opts.ItemStyleOpts(
            color=JsCode(
                """new echarts.graphic.LinearGradient(1, 0, 0, 0, 
                 [{offset: 0,color: 'rgb(255,99,71)'}, {offset: 1,color: 'rgb(32,178,170)'}])"""
            )
        )
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="商家上線的商品數目Top20"),
        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
        legend_opts=opts.LegendOpts(is_show=True))
    .reversal_axis()
)
bar1.render_notebook()
總體價格區間
pie1 = (
    Pie(init_opts=opts.InitOpts(theme='dark',width='1000px',height='600px'))
    
    .add('', datas_pair, radius=['35%', '60%'])
    .set_global_opts(
        title_opts=opts.TitleOpts(title='不同價格區間的銷售額整體表現'), 
        legend_opts=opts.LegendOpts(orient='vertical', pos_top='15%', pos_left='2%')
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{d}%"))
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="樂高、奧特曼、高達\n\n價格區間", 
            pos_left='center', 
            pos_top='center',
            title_textstyle_opts=opts.TextStyleOpts(
                color='#F0F8FF', 
                font_size=20, 
                font_weight='bold'
            ),
        )
    )
    .set_colors(['#EF9050', '#3B7BA9', '#6FB27C', '#FFAF34', '#D8BFD8', '#00BFFF', '#7FFFAA'])
)
pie1.render_notebook() 
單價最高的商品Top20
bar=(
    Bar(init_opts=opts.InitOpts(height='500px',width='1000px',theme='dark'))
    .add_xaxis(price_top.index.tolist())
    .add_yaxis(
        '單價最高的商品',
        price_top.values.tolist(),
        label_opts=opts.LabelOpts(is_show=True,position='top'),
        itemstyle_opts=opts.ItemStyleOpts(
            color=JsCode("""new echarts.graphic.LinearGradient(
            0, 0, 0, 1,[{offset: 0,color: 'rgb(255,99,71)'}, {offset: 1,color: 'rgb(32,178,170)'}])
            """
            )
        )
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title='單價最高的商品詳細柱狀圖'),
            xaxis_opts=opts.AxisOpts(name='玩具名稱',
            type_='category',                                           
            axislabel_opts=opts.LabelOpts(rotate=90),
        ),
        yaxis_opts=opts.AxisOpts(
            name='單價/元',
            min_=0,
            max_=39980.0,
            splitline_opts=opts.SplitLineOpts(is_show=True,linestyle_opts=opts.LineStyleOpts(type_='dash'))
        ),
        tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross')
    )

    .set_series_opts(
        markline_opts=opts.MarkLineOpts(
            data=[
                opts.MarkLineItem(type_='average',name='均值'),
                opts.MarkLineItem(type_='max',name='最大值'),
                opts.MarkLineItem(type_='min',name='最小值'),
            ]
        )
    )
)
bar.render_notebook()