df.dropna(inplace = True)
#提取出2011-2016年的数据,不过在预处理数据的时候已经整理好了
df = df[(2011 <= df['year'])&(df['year'] <=2016)]
df.reset_index(inplace = True)
T2.对比2011-2016年期间改编电影和原创电影每年平均票房收入,电影的其中一个keywords为“based on novel”,则它为改编电影,否则为原创电影。(使用pyecharts)
1.keywords里面有 “based on novel”是改编电影
2.提取出 keywords里面有 “based on novel”的数据统计电影数量(按年份分组求和)
3提取出 keywords是里面有 “based on novel”的数据统计票房总收入revenue(按年份分组求和)
4.总票房收入/电影数量=每一年 改编电影 平均票房收入
5.原创电影步骤同上
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar
import warnings
# 忽略警告。pandas很多时候会弹出警告,说某条命令即将在新版本中过期,建议换用新命令。如不想看到警告信息,可用此设置忽略
warnings.filterwarnings('ignore')
df = pd.read_excel('第15周课堂作业(预处理后的数据)T2.xlsx')
df.head()
df.dropna(inplace = True)
#提取出2011-2016年的数据,不过在预处理数据的时候已经整理好了
df = df[(2011 <= df['year'])&(df['year'] <=2016)]
df.reset_index(inplace = True)
for i in range(0,df.shape[0]):
if 'based on novel' in df.loc[i,'keywords']:
df.loc[i,'adapted'] = df.loc[i,'revenue']
else:
df.loc[i,'original'] = df.loc[i,'revenue']
#查看df数据
df
genre_year = df[['adapted','original']]
genre_year.index = df['year']
#将数据求平均值后 保留两位小数
rev_df = round(genre_year.groupby('year').mean()/10000,2)
rev_df
#rev_df的年份 转换为列表 获取年份
years = rev_df.index.tolist()
years = [str(i)for i in years]
years
years = ['2011', '2012', '2013', '2014', '2015', '2016']
#画图
bar = (
Bar()
.add_xaxis(years)
.add_yaxis('改编电影',rev_df['adapted'].tolist())
.add_yaxis('原创电影',rev_df['original'].tolist())
.set_global_opts(title_opts=opts.TitleOpts(title="柱形图"),
yaxis_opts=opts.AxisOpts(
#y轴标签名称
name="平均票房收入(万元)",
#y轴标签设置为竖直居中
name_location="center",
#y轴标签与y轴之间距离
name_gap=60))
.render('改编与原创.html')
)
pyecharts图形效果: