友情提示:本文共有 936 个字,阅读大概需要 2 分钟。
上一篇文章(Python爬虫帮助解决挑选电影费时费力的烦恼),我们对腾讯视频中的电影按照“豆瓣好评”的方式进行了数据爬虫,获取了大约5000部电影的详情数据,解决了选择电影时比较浪费时间的问题。本篇文章将基于上一篇文章的数据,来探究一下这5000部电影究竟怎么样呢?对这5000部作品,该如何去做数据分析呢?
用数据说话,是数据分析的意义所在,整个数据分析的过程分为三步:
数据读取
数据预处理
数据可视化及数据分析
以下是具体步骤和代码实现:
数据读取
在上一篇文章中,我们已经获取了数据,没有看过的同学可以进行查看,数据包括全量电影数据,更细粒度的拆解数据(年代与区域、类型与区域等),对于数据的采集,个人建议是尽量做到“全“、“细”,方便进行数据拆解与数据对比分析,对于电影数据的读取就相对比较简单了。
import pandas as pd##------------数据读取--------------#全量数据film_all_data = pd.read_csv("all_data.csv")#电影与区域的关系film_area_data = pd.read_csv("film_area.csv")#电影与类型的关系film_type_data = pd.read_csv("film_type.csv")#大陆电影与类型的关系film_type_china_data = pd.read_csv("film_type_china.csv")#电影与年代的关系film_year_data = pd.read_csv("film_year.csv")#大陆电影与年代的关系film_year_data = pd.read_csv("film_year_china.csv")
数据预处理
无论是做数据分析还是数据挖掘,数据预处理都是非常重要的一环,在数据预处理阶段,处理方法有很多,本文主要使用方法为:数据插补、脏数据删除、数据格式转换、数据行列转换等,上述处理方法在之前文章中都有涉及。
# 格式转换def transform_score(x): if x >=9.5: return "[9.5,)" elif x>=9 and x <9.5: return "[9,9.5)" elif x>=8.5 and x <9: return "[8.5,9)" elif x>=8 and x <8.5: return "[8,8.5)" else: return "(,8)"# 电影时间处理def film_time2min(t): if len(str(t))==5: m,s = t.strip().split(":") return int(m) elif len(str(t))==8: h,m,s = t.strip().split(":") return int(h) * 60 + int(m) else: return 0film_all_data["score_interval"] = film_all_data.apply(lambda x :transform_score(x["film_score"]),axis=1)film_all_data["film_caption"]= film_all_data["film_caption"].apply(lambda x :film_time2min(str(x)))film_all_data["is_vip"] = film_all_data["is_vip"].fillna("正常播放")film_all_data.drop(columns=["Unnamed: 0","film_score","img_href","film_href"],inplace=True)数据可视化与数据分析
我们使用pyecharts进行数据可视化,安装命令pip install pyechars,下面进行数据可视化与数据分析。
我们使用pyecharts进行数据可视化,安装命令pip install pyechars,下面进行数据可视化与数据分析。
代码实现:
bar = Bar(init_opts=opts.InitOpts(width="750px",))bar.add_xaxis(film_all_data_group["score_interval"].to_list())bar.add_yaxis("豆瓣好评",film_all_data_group["score_interval_num"].to_list())bar.set_global_opts(title_opts=opts.TitleOpts(), visualmap_opts=opts.VisualMapOpts(max_=50), toolbox_opts=opts.ToolboxOpts())
5000部电影中,VIP电影占比为55.7%,无需付费电影占比37.5%,VIP用券、独播、付费总体占比为7%,这么多的好作品,没有VIP会员的话,可能就没福享受了。
代码实现
from pyecharts.charts import Piefrom pyecharts import options as optspie1 = Pie(init_opts=opts.InitOpts(width="750px",))pie1.add("", [*zip(["VIP","VIP用券","付费","正常播放","独播"],[0.557229,0.016466,0.008233,0.375502,0.042570])], radius=["35%","70%"])pie1.set_global_opts(title_opts=opts.TitleOpts(), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"), toolbox_opts=opts.ToolboxOpts())pie1.set_series_opts(label_opts=opts.LabelOpts(formatter="{c}%"))pie1.set_colors(["#D7655A", "#FFAF34", "#3B7BA9", "#EF9050", "#6FB27C"])
全球电影优秀作品人物
在全球电影创作优秀作品的演员中,放眼望去都是外国人,仔细查看,还是有不少中国优秀演员的身影,刘德华、周星驰、刘青云、洪金宝等,除了在词云中看到的之外,周润发、梁家辉、古天乐、李连杰的表现的也是可圈可点的。
代码实现
##词云def get_comment_word(df): # 集合形式存储-去重 stop_words = set() print(stop_words) # 加载停用词 cwd = os.getcwd() stop_words_path = cwd + "stop_words.txt" print(stop_words_path) with open(stop_words_path, "r", encoding="ISO-8859-1") as sw: for line in sw.readlines(): stop_words.add(line.strip()) print(stop_words) # 添加停用词 stop_words.add("主演") # 合并评论信息 df_comment_all = df["film_actor"].str.cat() # 使用TF-IDF算法提取关键词 word_num = jieba.analyse.extract_tags(df_comment_all, topK=300, withWeight=True, allowPOS=()) print(word_num) # 做一步筛选 word_num_selected = [] # 筛选掉停用词 for i in word_num: if i[0] not in stop_words: word_num_selected.append(i) else: pass return word_num_selectedkey_words = get_comment_word(film_all_data)key_words = pd.DataFrame(key_words, columns=["words","num"]) key_words.head()
优秀作品产生时间
从中国优秀作品的趋势图来看,2016、2017年产生了较多优秀的作品,与全球优秀作品的趋势基本是一致的,从上图我们可以看出,随着中国电影市场的迅速发展,优秀作品在全球的占比权重在进一步增加。
代码实现
line1 = Line(init_opts=opts.InitOpts(width="750px",))line1.add_xaxis(["80年代","90年代","2005-2000","2010-2006","2013-2011","2014","2015","2016","2017","2018","2019"])line1.add_yaxis("年代", [270,1591,1397,1999,1704,560,712,1014,734,456,405], areastyle_opts=opts.AreaStyleOpts(opacity=0.5), label_opts=opts.LabelOpts(is_show=False))line1.set_global_opts(title_opts=opts.TitleOpts(), toolbox_opts=opts.ToolboxOpts(), visualmap_opts=opts.VisualMapOpts(max_=200))总结
本文从整体评分、付费类型、贡献优秀作品词云、优秀作品产生时间等不同视角来展示5000部电影的现状,如果我们有更多的精力,可以进行更细粒度的分析与拆解。
收集不易,本文《以腾讯5000部电影为例 告诉你Python数据分析该怎么做》知识如果对你有帮助,请点赞收藏并留下你的评论。