可视化链接https://public.tableau.com/profile/.5458#!/vizhome/1_2734/Q2
从The movie DB上获得一份电影数据进行可视化
提出问题:
- 1:电影类型是如何随着时间的推移发生变化的?
- 2:Universal Pictures 和 Paramount Pictures 之间的对比情况如何?
- 3:改编电影和原创电影的对比情况如何?
- 4: 发行年份跟收益的关系
数据各字段的意思
- id:标识号
• imdb_id:IMDB 标识号
• popularity:在 Movie Database 上的相对页面查看次数
• budget:预算(美元)
• revenue:收入(美元)
• original_title:电影名称
• cast:演员列表,按 | 分隔,最多 5 名演员
• homepage:电影首页的 URL
• director:导演列表,按 | 分隔,最多 5 名导演
• tagline:电影的标语
• keywords:与电影相关的关键字,按 | 分隔,最多 5 个关键字
• overview:剧情摘要
• runtime:电影时长
• genres:风格列表,按 | 分隔,最多 5 种风格
• production_companies:制作公司列表,按 | 分隔,最多 5 家公司
• release_date:首次上映日期
• vote_count:投票数
• vote_average:平均投票数
• release_year:发行年份
• budget_adj:根据通货膨胀调整的预算(2010 年,美元)
• revenue_adj:根据通货膨胀调整的收入(2010 年,美元)
导入模块
import pandas as pd
import numpy as np
加载数据
df=pd.read_csv('/Users/zhongyaode/Desktop/movies.csv')
#查看数据基本统计数据
df.describe()
#查看字段的数据类型及行数
df.info()
#显示前五行数据
df.head()
#选取需要的字段
dd=df[['id','budget','revenue','genres','production_companies','vote_count','release_year','keywords','original_title']]
dd.info()
处理缺失值
#删除有缺失值的行
dd.dropna(axis=0).info()
#分列字段 genres字段
split_genres=df['genres'].str.split('|',expand=True)
split_genres['id']=df['id']#把df的id字段赋值给split_genres
merged_back=dd.merge(split_genres)#根据字段id进行连接
#merge相当于mysqle的join 进行表连接
melt的官方文档https://pandas.pydata.org/pandas-docs/stable/generated/pandas.melt.html
melted=pd.melt(
merged_back,id_vars=['id','release_year'],
value_vars=[0,1,2,3,4],value_name='genres').drop('variable',axis=1).dropna()
melted.head()
#输出melted
melted.to_csv('id_year_genres.csv',index=False)
处理production_companies字段
#拆分
dd_production=dd['production_companies'].str.split('|',expand=True)
dd_production['id']=dd['id']
merge_backed=dd_production.merge(dd)
#用melt函数
melted_production=pd.melt(merge_backed,id_vars=['id','budget','revenue','release_year'],
value_vars=[0,1,2,3,4],value_name='production_companies'
).drop('variable',axis=1).dropna()
#筛选数据
melted_U_P=melted_production[(melted_production.production_companies=='Universal Pictures')|(melted_production.production_companies=='Paramount Pictures')]
melted_U_P.to_csv('melted_U_P.csv',index
=False)
处理keywords字段
#拆分字段
dd_keywords=dd['keywords'].str.split('|',expand=True)
dd_keywords['id']=dd['id']
dd_merge_keywords=dd.merge(dd_keywords)
#运用melt函数
if_novel=pd.melt(dd_merge_keywords,id_vars=['id','budget','revenue','release_year','original_title','vote_count'],
value_vars=[0,1,2,3,4],value_name='keywords').drop('variable',axis=1).dropna()
#再对keywords进行处理,值是based on novel的返回based on novel否则返回Not_novel
def peng(data):
if data=='based on novel':
return 'based on novel'
else:
return 'Not_novel'
if_novel['keywords']=if_novel['keywords'].apply(lambda x: peng(x))
#这里用python处理当是练习了,其实用tablea的创建组方法能非常简单的处理好
#输出
if_novel.to_csv('if_novel.csv',index=False)
接下来用非常好玩的tableau探索数据
tableau交互可视化的链接https://public.tableau.com/profile/.5458#!/vizhome/1_2734/Q1
参考资料 melt的官方文档https://pandas.pydata.org/pandas-docs/stable/generated/pandas.melt.html
以及tableau的官网教程
来几张工作仪和story
图片 1.png 图片 2.png 图片 3.png 图片 4.png 图片 5.png 6.png 7.png以下进行的是尝试把四个文件合并到一起的方式·
df_genres=df['genres'].str.split('|',expand=True)
df_genres.info()
df_genres['id']=df['id']
df_genres['production_companies']=df['production_companies']
df_genres['keywords']=df['keywords']
#df.merge(df_genres).info()
df_pro=pd.melt(df_genresed,id_vars=['production_companies','id','keywords'],value_vars=[0,1,2,3,4],
value_name='genres').drop('variable',axis=1).dropna()
df_product=df_pro['production_companies'].str.split('|',expand=True)
df_product['id']=df_pro['id']
df_product['genres']=df_pro['genres']
df_product['keywords']=df_pro['keywords']
df_genres_pro=pd.melt(df_product,id_vars=['id','genres','keywords'],value_vars=[0,1,2,3,4],
value_name='production_companies').drop('variable',axis=1).dropna()
df_k_g_p=df_genres_pro['keywords'].str.split('|',expand=True)
df_k_g_p['id']=df_genres_pro['id']
df_k_g_p['genres']=df_genres_pro['genres']
df_k_g_p['production_companies']=df_genres_pro['production_companies']
ddd=pd.melt(df_k_g_p,id_vars=['id','genres','production_companies'],value_vars=[0,1,2,3,4],
value_name='keywords').drop('variable',axis=1).dropna()
ddd.info()
ddd.head()
movie=df
merged_split
split_companies=movie['keywords'].str.split('|',expand=True)
split_companies['id']=movie['id']
merged_split=movie.merge(split_companies)
key_df=pd.melt(merged_split,id_vars=['id','revenue','budget','release_date','genres','production_companies'],value_vars=[0,1,2,3,4],value_name='keyword').drop('variable',axis=1).dropna()
split_companies=movie['keywords'].str.split('|',expand=True)
split_companies['id']=movie['id']
merged_split=movie.merge(split_companies)
key_df=pd.melt(merged_split,id_vars=['id','revenue','budget','release_date','genres','production_companies'],value_vars=[0,1,2,3,4],value_name='keyword').drop('variable',axis=1).dropna()
split_companies=key_df['production_companies'].str.split('|',expand=True)
split_companies['id']=key_df['id']
# merged_split=key_df.merge(split_companies,on='id',how='left')
merged_split=key_df.merge(split_companies)
pp=pd.melt(merged_split,id_vars=['id','release_date','genres','keyword','revenue','budget'],value_vars=[0,1,2,3,4],value_name='production_company').drop('variable',axis=1).dropna()
movie=df.drop(['imdb_id','popularity','vote_average','original_title','cast','homepage','director','tagline','overview','runtime','vote_count','release_year','budget_adj','revenue_adj'],axis=1)
split_companies=movie['keywords'].str.split('|',expand=True)
split_companies['id']=movie['id']
merged_split=movie.merge(split_companies)
key_df=pd.melt(merged_split,id_vars=['id','revenue','budget','release_date','genres','production_companies'],value_vars=[0,1,2,3,4],value_name='keyword').drop('variable',axis=1).dropna()
split_genres=key_df['genres'].str.split('|',expand=True)
split_genres['id']=key_df['id']
merged_split=key_df.merge(split_genres)
genre=pd.melt(merged_split,id_vars=['id','release_date','production_companies','keyword','revenue','budget'],value_vars=[0,1,2,3,4],value_name='genre').drop('variable',axis=1).dropna()
genred=genre[:10000]
split_companies=genred['production_companies'].str.split('|',expand=True)
split_companies['id']=genred['id']
#merged_split=genre.merge(split_companies)
merg=genred.merge(split_companies,on='id',how='left')
#merged_split[:1]
pp=pd.melt(merg,id_vars=['id','release_date','genre','keyword','revenue','budget'],value_vars=[0,1,2,3,4],value_name='production_company').drop('variable',axis=1).dropna()
pp.info()
movie=df.drop(['imdb_id','popularity','vote_average','original_title','cast','homepage','director','tagline','overview','runtime','vote_count','release_year','budget_adj','revenue_adj'],axis=1)
split_companies=movie['keywords'].str.split('|',expand=True)
split_companies['id']=movie['id']
split_companies
merged_split=movie.merge(split_companies)
key_df=pd.melt(merged_split,id_vars=['id','revenue','budget','release_date','genres','production_companies'],value_vars=[0,1,2,3,4],value_name='keyword').drop('variable',axis=1).dropna()
split_genres=key_df['genres'].str.split('|',expand=True)
split_genres['id']=key_df['id']
merged_split=key_df.merge(split_genres)
genre_dff=pd.melt(merged_split,id_vars=['id','release_date','production_companies','keyword','revenue','budget'],value_vars=[0,1,2,3,4],value_name='genre').drop('variable',axis=1).dropna()
genre_df=genre_dff[:10000]
split_companies=genre_df['production_companies'].str.split('|',expand=True)
split_companies['id']=genre_df['id']
merged_split=genre_df.merge(split_companies,on='id',how='left')
p=pd.melt(merged_split,id_vars=['id','release_date','genre','keyword','revenue','budget'],value_vars=[0,1,2,3,4],value_name='production_company').drop('variable',axis=1).dropna()
网友评论