大家好,我是辣條,
終于要開始資料分析系列的文章了,相比爬蟲來說技術維度還是上升一個層次的,我的文章輸出呢會更新實戰專案系列,和知識點詳解總結系列,分別在兩個專欄,目標是短期實作爬蟲和資料分析實戰專案100個案例,

涉及到的庫:
Pandas — 資料處理 Pyecharts — 資料可視化 jieba — 分詞 collections — 資料統計
可視化部分:
折線圖 — Line 柱狀圖 — Bar 餅狀圖 — Pie 日歷圖 — Calendar 詞云圖 — WordCloud 地圖 — Geo
白蛇2:青蛇劫起
*劇情簡介:*
2021年7月23日, 白蛇2:青蛇劫起在大陸上映,主要講述南宋末年,小白為救許仙水漫金山,終被法海壓在雷峰塔下,小青則意外被法海打入詭異的修羅城幻境,幾次危機中小青被神秘蒙面少年所救,小青帶著出去救出小白的執念歷經劫難與成長,同蒙面少年一起尋找離開辦法的故事
執行環節 Notebook
安裝第三方包
!pip install pyecharts
!pip install pandas
!pip install numpy
匯入第三方包
import pandas as pd
import numpy as np
from pyecharts.charts import *
from PIL import Image
from collections import Counter
from pyecharts import options as opts # 可視化配置項
from pyecharts.commons.utils import JsCode # 用來跑js代碼的
from pyecharts.globals import ThemeType,SymbolType,ChartType # 可視化主題樣式
讀取資料
df = pd.read_excel("./白蛇2.xlsx")
df.head(10) # 查看前 10行
| id | 用戶名 | 城市 | 評分 | 評論 | 評論時間 | |
|---|---|---|---|---|---|---|
| 0 | 1142669584 | 淇桐糯米飯啦 | 來賓 | 5.0 | 劇情非常有吸引力,看個影片片給了我驚喜 | 2021-08-31 23:56:30 |
| 1 | 1142662178 | LnV14610189 | 西寧 | 5.0 | 畫面感超強! | 2021-08-31 23:36:00 |
| 2 | 1142666877 | Alo861902585 | 廣州 | 5.0 | 和一銜接的很不錯,精彩 | 2021-08-31 23:34:41 |
| 3 | 1142660216 | Y, | 西安 | 4.0 | 畫面人物沒的說,你永遠可以相信追光,劇情還算順暢整體節奏也還可以,推薦觀看——! | 2021-08-31 23:30:56 |
| 4 | 1142669423 | 想為你攬月 | 鳳臺 | 5.0 | 不錯哦,雖然小青小白的執念有一丟丟丟丟丟牽強(還好),如果能細說一下牛魔的執念劇情會更完美,... | 2021-08-31 23:27:28 |
| 5 | 1142669422 | 啊卡哇啊呀啊呀 | 日照 | 5.0 | 不錯不錯,感覺越來越有看頭了 | 2021-08-31 23:27:12 |
| 6 | 1142669404 | Lfz9696 | 永州 | 4.5 | 還可以,很不錯 | 2021-08-31 23:23:30 |
| 7 | 1142666812 | Tenacity | 廣州 | 4.0 | 電影還行,就是隔壁有個男的一直抖腿, | 2021-08-31 23:22:23 |
| 8 | 1142661206 | CQE579669148 | 烏魯木齊 | 5.0 | 好好看,推薦 | 2021-08-31 23:16:22 |
| 9 | 1142668420 | 胖胖的嗩吶 | 伊犁 | 5.0 | 劇情有點沒看懂,但是影片特效非常棒!情節挺感人的, | 2021-08-31 23:06:36 |
資料清洗
缺失值查看
df.isnull().sum()
id 0
用戶名 1
城市 0
評分 0
評論 0
評論時間 0
dtype: int64
查看發現存在一個確實情況
用戶名存在一條缺失,其他各列資料完整,用“未知”填充空值:
df['用戶名'].fillna('未知', inplace=True)
df.isnull().sum()
Pyecharts資料可視化
評分等級分布
# 線性漸變
color_js = """new echarts.graphic.LinearGradient(0, 0, 1, 0,
[{offset: 0, color: '#009ad6'}, {offset: 1, color: '#ed1941'}], false)"""
?
df_star = df.groupby('評分')['評論'].count()
df_star = df_star.sort_values(ascending=True)
x_data = [str(i) for i in list(df_star.index)]
y_data = df_star.values.tolist()
b1 = (
Bar()
.add_xaxis(x_data)
.add_yaxis('',y_data,itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js)))
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position='right'))
.set_global_opts(
yaxis_opts=opts.AxisOpts(name='評分等級'),
xaxis_opts=opts.AxisOpts(name='人/次'),
title_opts=opts.TitleOpts(title='評分等級分布',pos_left='45%',pos_top="5%"),
legend_opts=opts.LegendOpts(type_="scroll", pos_left="85%",pos_top="28%",orient="vertical")
)
)
?
df_star = df.groupby('評分')['評論'].count()
x_data = [str(i) for i in list(df_star.index)]
y_data = df_star.values.tolist()
p1 = (
Pie(init_opts=opts.InitOpts(width='800px', height='600px'))
.add(
'',
[list(z) for z in zip(x_data, y_data)],
radius=['10%', '30%'],
center=['65%', '60%'],
label_opts=opts.LabelOpts(is_show=True),
)
.set_colors(["blue", "green", "#800000", "red", "#000000", "orange", "purple", "red", "#000000", "orange", "purple"])
.set_series_opts(label_opts=opts.LabelOpts(formatter='評分{b}: {c} \n ({d}%)'),position="outside")
)
?
b1.overlap(p1)
b1.render_notebook()
5.0的評分占比達到了56%,超過了半數觀眾打出了五星好評,四星以上好評更是達到了85%之多,看來大家對這部動漫還是高度認可的,
2021.08.01-2021.08.31 每天評論量分布:
# 設定樣式
# 加載的js代碼 做樣式 主要是 顏色 和主題
color_js = """new echarts.graphic.LinearGradient(0, 1, 0, 0,
[{offset: 0, color: '#009ad6'}, {offset: 1, color: '#ed1941'}], false)"""
?
area_color_js = (
"new echarts.graphic.LinearGradient(0, 0, 0, 1, "
"[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)"
)
?
# 設定引數
linestyle_dic = { 'normal': {
'width': 2,
'shadowColor': '#696969',
'shadowBlur': 10,
'shadowOffsetY': 10,
'shadowOffsetX': 10,
}
}
?
# 轉時間格式
df['評論時間'] = pd.to_datetime(df['評論時間'], format='%Y/%m/%d %H:%M:%S')
# 每日評論量
df['評論時間'] = pd.to_datetime(df['評論時間'], format='%Y/%m/%d %H:%M:%S')
df_day = df.groupby(df['評論時間'].dt.day)['評論'].count() # 根據評論時間 獲取評論的數量(count)
day_x_data = [str(i) for i in list(df_day.index)] # x軸
day_y_data = df_day.values.tolist() # 輸出成串列 y軸
line1 = (
Line(init_opts=opts.InitOpts(bg_color=JsCode(color_js))) # 線性可視化
.add_xaxis(xaxis_data=day_x_data) # 添加x軸資料
.add_yaxis( # 添加y軸資料
series_name="", # y軸名稱
y_axis=day_y_data, # 資料
is_smooth=True,
is_symbol_show=True,
symbol="circle",
symbol_size=6,
linestyle_opts=opts.LineStyleOpts(color="#fff"), # 配置y軸線
label_opts=opts.LabelOpts(is_show=True, position="top", color="white"), # y軸標簽
itemstyle_opts=opts.ItemStyleOpts(
color="red", border_color="#fff", border_width=3
),
tooltip_opts=opts.TooltipOpts(is_show=False),
areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="八月每日評論量",
pos_top="5%",
pos_left="center",
title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16),
),
xaxis_opts=opts.AxisOpts(
type_="category",
boundary_gap=True,
axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63"),
axisline_opts=opts.AxisLineOpts(is_show=False),
axistick_opts=opts.AxisTickOpts(
is_show=True,
length=25,
linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
),
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
),
),
yaxis_opts=opts.AxisOpts(
type_="value",
position="left",
axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"),
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(width=2, color="#fff")
),
axistick_opts=opts.AxisTickOpts(
is_show=True,
length=15,
linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
),
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
),
),
legend_opts=opts.LegendOpts(is_show=False),
)
)
line1.render_notebook()
每天評論量在8月1日達到峰值(資料不包含7月份),隨著時間的推移評論數量逐漸減少,這也符合一般電影觀影規律,
每小時評論量
統計的是2021.08.01-2021.08.31這31天每小時天評論量總和(如果感興趣可以單獨查看某一天24小時影評數量分布,按日期篩選即可)
df_hour = df.groupby(df['評論時間'].dt.hour)['評論'].count()
hours_x_data = [str(i) for i in list(df_hour.index)]
hours_y_data = df_hour.values.tolist()
line1 = (
# Line(init_opts=opts.InitOpts(bg_color=JsCode(color_js)))
Line(init_opts=opts.InitOpts(width='1000px', height='400px'))
.add_xaxis(xaxis_data=hours_x_data)
.add_yaxis(
series_name="",
y_axis=hours_y_data,
is_smooth=True,
is_symbol_show=True,
symbol="circle",
symbol_size=6,
linestyle_opts=opts.LineStyleOpts(color="#fff"),
label_opts=opts.LabelOpts(is_show=True, position="top", color="white"),
itemstyle_opts=opts.ItemStyleOpts(
color="red", border_color="#fff", border_width=3
),
tooltip_opts=opts.TooltipOpts(is_show=False),
areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1),
)
.set_series_opts(
linestyle_opts=linestyle_dic,label_opts=opts.LabelOpts(font_size=12, color='red' ),
markpoint_opts=opts.MarkPointOpts(
data=[opts.MarkPointItem(type_="max",itemstyle_opts=opts.ItemStyleOpts(
color="#06FFD7", border_width=3)),
opts.MarkPointItem(type_="min",itemstyle_opts=opts.ItemStyleOpts(
color="#06FFD7", border_width=3))],
symbol_size=[65, 50],
label_opts=opts.LabelOpts(position="inside", color="red", font_size=10)
),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="每小時評論量",
pos_top="5%",
pos_left="center",
title_textstyle_opts=opts.TextStyleOpts(color="#EB1934", font_family='STKaiti', font_size=20),
),
xaxis_opts=opts.AxisOpts(
type_="category",
boundary_gap=True,
axislabel_opts=opts.LabelOpts(margin=30, color="#EB1934"),
axisline_opts=opts.AxisLineOpts(
is_show=False,
linestyle_opts=opts.LineStyleOpts(color="#EB1934")
),
axistick_opts=opts.AxisTickOpts(
is_show=False,
length=25,
linestyle_opts=opts.LineStyleOpts(color="#EB1934"),
),
),
yaxis_opts=opts.AxisOpts(
type_="value",
position="left",
axislabel_opts=opts.LabelOpts(is_show=False, margin=20, color="#EB1934"),
axisline_opts=opts.AxisLineOpts(
is_show=False,
linestyle_opts=opts.LineStyleOpts(width=2, color="#EB1934")
),
axistick_opts=opts.AxisTickOpts(
is_show=False,
length=10,
linestyle_opts=opts.LineStyleOpts(color="#EB1934"),
),
splitline_opts=opts.SplitLineOpts(
is_show=False, linestyle_opts=opts.LineStyleOpts(color="#EB1934")
),
),
legend_opts=opts.LegendOpts(is_show=False),
graphic_opts=[
opts.GraphicImage(
graphic_item=opts.GraphicItem(
id_="logo", z=-10, bounding="raw", origin=[50, 100]
),
graphic_imagestyle_opts=opts.GraphicImageStyleOpts(
image="./12.jpg",
width=1000,
height=400,
opacity=0.3,
),
)
],
)
)
# line1.render_notebook()
# 背景圖在本地可以顯示,但是平臺只顯示折線圖無背景,可以把代碼拷貝到本地運行
Image.open("./2.png")
從小時分布來看,大家一般選擇在下午到晚上評論的比較多,尤其是在17:00以后,大家在作業時段還都是比較敬業的,第二次評論峰值在22:00,這個時間段是熬夜青年比較活躍的時段,小伙伴們的作息時間都比較靠后,
3.4 一周各天評論量
統計的是2021.08.01-2021.08.31這31天每周各天評論量的總和:
?
# 增加欄位'星期'
dic = {1:'星期一',2:'星期二',3:'星期三',4:'星期四',5:'星期五',6:'星期六',7:'星期日'}
df['星期'] = df['評論時間'].dt.dayofweek+1
df['星期'] = df['星期'].map(dic)
df.head(5
)
?
| id | 用戶名 | 城市 | 評分 | 評論 | 評論時間 | 星期 | |
|---|---|---|---|---|---|---|---|
| 0 | 1142669584 | 淇桐糯米飯啦 | 來賓 | 5.0 | 劇情非常有吸引力,看個影片片給了我驚喜 | 2021-08-31 23:56:30 | 星期二 |
| 1 | 1142662178 | LnV14610189 | 西寧 | 5.0 | 畫面感超強! | 2021-08-31 23:36:00 | 星期二 |
| 2 | 1142666877 | Alo861902585 | 廣州 | 5.0 | 和一銜接的很不錯,精彩 | 2021-08-31 23:34:41 | 星期二 |
| 3 | 1142660216 | Y, | 西安 | 4.0 | 畫面人物沒的說,你永遠可以相信追光,劇情還算順暢整體節奏也還可以,推薦觀看——! | 2021-08-31 23:30:56 | 星期二 |
| 4 | 1142669423 | 想為你攬月 | 鳳臺 | 5.0 | 不錯哦,雖然小青小白的執念有一丟丟丟丟丟牽強(還好),如果能細說一下牛魔的執念劇情會更完美,... | 2021-08-31 23:27:28 | 星期二 |
# 一周各天評論量
dic = {1:'星期一',2:'星期二',3:'星期三',4:'星期四',5:'星期五',6:'星期六',7:'星期日'}
df['星期'] = df['評論時間'].dt.dayofweek+1
df1 = df.sort_values('星期',ascending=True)
df_week = df1.groupby(['星期'])['評論'].count()
week_x_data = [dic[i] for i in list(df_week.index)]
week_y_data = df_week.values.tolist()
line1 = (
Line(init_opts=opts.InitOpts(bg_color=JsCode(color_js)))
.add_xaxis(xaxis_data=week_x_data)
.add_yaxis(
series_name="",
y_axis=week_y_data,
is_smooth=True,
is_symbol_show=True,
symbol="circle",
symbol_size=6,
linestyle_opts=opts.LineStyleOpts(color="#fff"),
label_opts=opts.LabelOpts(is_show=True, position="top", color="white"),
itemstyle_opts=opts.ItemStyleOpts(
color="red", border_color="#fff", border_width=3
),
tooltip_opts=opts.TooltipOpts(is_show=False),
areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="一周各天評論量",
pos_top="5%",
pos_left="center",
title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16),
),
xaxis_opts=opts.AxisOpts(
type_="category",
boundary_gap=True,
axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63"),
axisline_opts=opts.AxisLineOpts(is_show=False),
axistick_opts=opts.AxisTickOpts(
is_show=True,
length=25,
linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
),
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
),
),
yaxis_opts=opts.AxisOpts(
type_="value",
position="left",
axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"),
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(width=2, color="#fff")
),
axistick_opts=opts.AxisTickOpts(
is_show=True,
length=15,
linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
),
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
),
),
legend_opts=opts.LegendOpts(is_show=False),
)
)
line1.render_notebook()
從一周各天資料分布來看,每周一和每周天是大家評論的活躍時段,很有意思,一周的開始和一周的結束,在休中開始,在休閑中結束,
3.5 日歷圖
times = [x.strftime('%Y-%m-%d') for x in list(pd.date_range('20210801', '20210831'))]
data = [[times[index],day_y_data[index]] for index,item in enumerate( day_y_data)]
Cal = (
Calendar(init_opts=opts.InitOpts(width="800px", height="500px"))
.add(
series_name="八月份每日評論量分布情況",
yaxis_data=data,
calendar_opts=opts.CalendarOpts(
pos_top='20%',
pos_left='5%',
range_="2021-08",
cell_size=40,
# 年月日標簽樣式設定
daylabel_opts=opts.CalendarDayLabelOpts(name_map="cn",
margin=20,
label_font_size=14,
label_color='#EB1934',
label_font_weight='bold'
),
monthlabel_opts=opts.CalendarMonthLabelOpts(name_map="cn",
margin=20,
label_font_size=14,
label_color='#EB1934',
label_font_weight='bold',
is_show=False
),
yearlabel_opts=opts.CalendarYearLabelOpts(is_show=False),
),
tooltip_opts='{c}',
)
.set_global_opts(
title_opts=opts.TitleOpts(
pos_top="2%",
pos_left="center",
title=""
),
visualmap_opts=opts.VisualMapOpts(
orient="horizontal",
max_=2000,
pos_bottom='10%',
is_piecewise=True,
pieces=[{"min": 1200},
{"min": 800, "max": 1200},
{"min": 500, "max": 800},
{"min": 300, "max": 500},
{"min": 80, "max": 300},
{"max": 80}],
range_color=["#F5F5F5", "#FFE4E1", "#FFCC99", "#F08080", "#CD5C5C", "#990000"]
),
legend_opts=opts.LegendOpts(is_show=True,
pos_top='5%',
item_width = 50,
item_height = 30,
textstyle_opts=opts.TextStyleOpts(font_size=16,color='#EB1934'),
legend_icon ='path://path://M621.855287 587.643358C708.573965 540.110571 768 442.883654 768 330.666667 768 171.608659 648.609267 42.666667 501.333333 42.666667 354.057399 42.666667 234.666667 171.608659 234.666667 330.666667 234.666667 443.22333 294.453005 540.699038 381.59961 588.07363 125.9882 652.794383 21.333333 855.35859 21.333333 1002.666667L486.175439 1002.666667 1002.666667 1002.666667C1002.666667 815.459407 839.953126 634.458526 621.855287 587.643358Z'
),
)
)
Cal.render_notebook()
3.6 角色熱度
主要人物:小白、小青、許仙、法海、司馬、孫姐、牛頭幫主、蒙面男子、寶青坊主、書生
roles=['小白','小青','許仙','法海','司馬','孫姐','牛頭幫主','蒙面男子','寶青坊主','書生']
content=''.join([str(i) for i in list(df['評論'])])
roles_num=[]
for role in roles:
count=content.count(role)
roles_num.append((role,count))
roles_num=pd.DataFrame(roles_num)
roles_num.columns=['名稱','出現次數']
roles_num
| 名稱 | 出現次數 | |
|---|---|---|
| 0 | 小白 | 1523 |
| 1 | 小青 | 2683 |
| 2 | 許仙 | 239 |
| 3 | 法海 | 396 |
| 4 | 司馬 | 112 |
| 5 | 孫姐 | 20 |
| 6 | 牛頭幫主 | 1 |
| 7 | 蒙面男子 | 3 |
| 8 | 寶青坊主 | 101 |
| 9 | 書生 | 4 |
# 線性漸變
color_js = """new echarts.graphic.LinearGradient(0, 1, 0, 0,
[{offset: 0, color: '#FFFFFF'}, {offset: 1, color: '#ed1941'}], false)"""
roles_num=roles_num.sort_values(by='出現次數',ascending=False)
roles_num=roles_num.reset_index(drop=True)
b2 = (
Bar()
.add_xaxis(list(roles_num['名稱']))
.add_yaxis('頻次', list(roles_num['出現次數']),itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js)))
.set_global_opts(title_opts=opts.TitleOpts(title='影評角色頻次分布',pos_top='2%',pos_left = 'center'),
legend_opts=opts.LegendOpts(is_show=False),
yaxis_opts=opts.AxisOpts(name="頻次",name_location='middle',name_gap=50,name_textstyle_opts=opts.TextStyleOpts(font_size=16)))
)
b2.render_notebook()
3.7 觀眾地域分布
cities = df['城市'].values.tolist()
data = Counter(cities).most_common(80)
geo = (
Geo(init_opts=opts.InitOpts(width="1000px", height="600px", bg_color="#404a59"))
.add_schema(maptype="china",
itemstyle_opts={
'normal': {
'shadowColor': 'rgba(0, 0, 0, .5)',
'shadowBlur': 5,
'shadowOffsetY': 0,
'shadowOffsetX': 0,
'borderColor': '#fff'
}
}
)
.add("評論數量", data,type_=ChartType.HEATMAP,)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="地理位置分布",pos_top="2%", pos_left="center",
title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16)),
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(
is_show=True,
is_piecewise=True,
min_ = 0,
max_ = 500,
split_number = 5,
series_index=0,
pos_bottom='5%',
pos_left='5%',
textstyle_opts=opts.TextStyleOpts(color="#fff"),
pieces=[
{'max':500, 'min':401, 'label':'401-500', 'color': '#990000'},
{'max':400, 'min':301, 'label':'301-400', 'color': '#CD5C5C'},
{'max':300, 'min':201, 'label':'201-300', 'color': '#F08080'},
{'max':200, 'min':101, 'label':'101-200', 'color': '#FFCC99'},
{'max':100, 'min':0, 'label':'0-100', 'color': '#FFE4E1'},
],
),
)
)
geo.render_notebook()
從地域分布圖來看,觀眾主要分布在北京、天津、上海、重慶、四川、廣東、云南等地,
行業資料:添加即可領取PPT模板、簡歷模板、行業經典書籍PDF,
面試題庫:歷年經典,熱乎的大廠面試真題,持續更新中,添加獲取,
學習資料:含Python、爬蟲、資料分析、演算法等學習視頻和檔案,添加獲取
交流加群:大佬指點迷津,你的問題往往有人遇到過,技識訓助交流,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/300757.html
標籤:python
上一篇:Python中的字串介紹(上)
