1 时间序列分析
也就是对不同导演电影上映的时间进行整理,就得要求两个字段的数据不能为空值,一个是导演字段,一个就是电影上映日期字段
1) 空白数据的清洗
df_year = df[(df['导演'].notnull()) & (df['上映日期'].notnull())][['电影名称','导演','豆瓣评分','上映日期']]
print(df_year)
–> 输出结果为:(只是截取部分数据)
2) 上映日期字段数据清洗
df_year['上映日期'] = df_year['上映日期'].str.replace(' ','')
df_year = df_year[df_year['上映日期'].str[0] == '2'].reset_index()
df_year['year'] = df_year['上映日期'].apply(lambda x: x[:4]).astype(np.int)
df_year.drop(['index','上映日期'],axis =1,inplace = True)
–> 输出结果为:(首先将上映日期里面的空格去掉,然后选取里面上映时间为2开头的数据并重新设置索引,之后提取上映的年份顺带着将字符串数据转化为整型数值,最后将不必要的字段给剔除)
3) 筛选导演数据
还是和之前的处理数据的方式一样,遍历‘导演’字段的数据,然后将导演全部加到列表里面,并进行去重
director_lst = []
for i in df_year['导演'].str.replace(' ','').str.split('/'):
director_lst.extend(i)
director_lst = list(set(director_lst))
print(f'一共有导演人数为{len(director_lst)}')
–> 输出结果为:
一共有导演人数为1485
4)查看作品大于10部的导演烂片情况
这里就是将所有产出大于10部电影的导演数据以字典的形式存放在列表中
lst_dir_lp = []
for i in director_lst:
datai = df_year[df_year['导演'].str.contains(i)]
if len(datai) > 10:
dic_dir_lp = {}
dir_lp = len(datai[datai['豆瓣评分']<4.3])
dir_count = len(datai)
lp_pre_i = dir_lp/dir_count
dic_dir_lp['dir'] = i
dic_dir_lp['dir_lp_num'] = dir_lp
dic_dir_lp['dir_count'] = dir_count
dic_dir_lp['dir_lp_pre'] = lp_pre_i
lst_dir_lp.append(dic_dir_lp)
然后将列表数据转化为DataFrame数据
df_dir_lp = pd.DataFrame(lst_dir_lp)
print(df_dir_lp)
–> 输出结果为:
5) 不同导演的电影产量和均分
def f3(data,diri):
datai = data[data['导演'].str.contains(diri)]
# 筛选数据
data_moviecount = datai[['year','电影名称']].groupby('year').count()
data_scoremean = datai[['year','豆瓣评分']].groupby('year').mean()
df_i = pd.merge(data_moviecount,data_scoremean,left_index = True,right_index = True)
df_i.columns = ['count','score']
df_i['size'] = df_i['count']*5
return(df_i)
dirdata1 = f3(df_year,'王晶')
dirdata2 = f3(df_year,'周伟')
dirdata3 = f3(df_year,'徐克')
dirdata4 = f3(df_year,'邓衍成')
比如查看王晶的产量,结果如下:
6) 最后通过bokeh绘图
from bokeh.models.annotations import BoxAnnotation
# 导入BoxAnnotation模块
output_file('导演电影产出情况.html')
hover = HoverTool(tooltips=[("该年电影均分", "@score"),
("该年电影产量","@count")])
p = figure(plot_width=900, plot_height=500, title="不同导演每年的电影产量及电影均分",
tools=[hover,'reset,xwheel_zoom,pan,crosshair,box_select'])
# 构建绘图空间
source1 = ColumnDataSource(dirdata1)
p.circle(x='year',y='score',source = source1,size = 'size',legend="王晶",fill_color = 'olive',fill_alpha = 0.7,line_color = None)
# 绘制散点图1
source2 = ColumnDataSource(dirdata2)
p.circle(x='year',y='score',source = source2,size = 'size',legend="周伟",fill_color = 'blue',fill_alpha = 0.7,line_color = None)
# 绘制散点图2
source3 = ColumnDataSource(dirdata3)
p.circle(x='year',y='score',source = source3,size = 'size',legend="徐克",fill_color = 'green',fill_alpha = 0.7,line_color = None)
# 绘制散点图3
source4 = ColumnDataSource(dirdata4)
p.circle(x='year',y='score',source = source4,size = 'size',legend="邓衍成",fill_color = 'gray',fill_alpha = 0.7,line_color = None)
# 绘制散点图4
bg = BoxAnnotation(top=4.4,fill_alpha=0.1, fill_color='red')
p.add_layout(bg)
# 绘制烂片分隔区域
p.xgrid.grid_line_dash = [10,4]
p.ygrid.grid_line_dash = [10,4]
p.legend.location = "top_right"
# 设置其他参数
show(p)
–> 输出结果为: