数据分析之解析电影中如下问题
作者:互联网
“”"
1.导演电影部数排名前5位的导演,分别导演了哪些电影Title,总票房多少?Revenue
2.导演电影最多的导演是否平分平均分最高?
3.科幻片票房最高的
4.哪种类型的影片数量最多
5.评分最高的影片分别是什么
“”"
数据如需下载下载下方链接,复制粘贴到你当前目录
链接:https://pan.baidu.com/s/1YCff3HE0sbBoHt5ruG7QnQ
提取码:g8yk
import pandas as pd
movie=pd.read_csv('IMDB.csv')
# 1.导演电影部数排名前5位的导演,分别导演了哪些电影Title,总票房多少?Revenue(没写好,找不到电影导演,只能暂时一步一步写)
# for i in movie['Director'].value_counts().head(5):
# print(movie['Director'].value_counts() == i)
print(movie['Director'].value_counts().head(5))
print('*'*100)
#查找多属性时需要用俩个中括号
print(movie[movie['Director'].str.contains('Ridley Scott')][['Title','Revenue (Millions)']])
print('*'*100)
print(movie[movie['Director'].str.contains('M. Night Shyamalan')][['Title','Revenue (Millions)']])
print('*'*100)
print(movie[movie['Director'].str.contains('David Yates')][['Title','Revenue (Millions)']])
print('*'*100)
print(movie[movie['Director'].str.contains('Paul W.S. Anderson')][['Title','Revenue (Millions)']])
print('*'*100)
print(movie[movie['Director'].str.contains('Michael Bay')][['Title','Revenue (Millions)']])
2.导演电影最多的导演是否平分最高?打印*号是为了能看清楚分界
# print(movie['Metascore'].max())
#遍历导演
for i in movie['Director']:
#找到导演电影最多的
if i == movie['Director'].value_counts().idxmax():
#打印它的评分并判断是否相等
print(movie[movie['Director']==i][['Metascore']].max())
print('*'*100)
print(movie[movie['Director']==i][['Metascore']].max() == movie['Metascore'].max())
print('*' * 100)
print(movie['Metascore'].max())
break
# 3.科幻片票房最高的票房,和第二个类似
#找到科幻片最大票房
# print(movie[movie['Genre'].str.contains('Sci-Fi')]['Revenue (Millions)'].max())
#找到最大票房,可以不用因为下方已经遍历
# print(movie['Revenue (Millions)'].max())
for j in movie['Revenue (Millions)']:
if j == movie[(movie['Genre'].str.contains('Sci-Fi'))]['Revenue (Millions)'].max():
print(movie[(movie['Genre'].str.contains('Sci-Fi'))]['Revenue (Millions)'].max())#写成print(j)也可以
break
# 4.哪种类型的影片数量最多
#1.创建列表存储影片类型
l=[]
for i in movie['Genre']:
#字符串类型,若直接遍历得到的是一个一个的字符,所以按','切割
m=i.split(',')
for j in m:
#然后遍历不在列表中添加到列表
if j not in l:
l.append(j)
#2.遍历类型并统计影片数量
#创建列表来收集影片数量并添加到列表中,然后判断最大值索引找出相应列表l的影片类型
m=[]
for k in l:
m.append(len(movie[movie['Genre'].str.contains(k)]))
print(l[m.index(max(m))])
# 5.评分最高的影片分别是什么
#1.找到评分最高的movie['Metascore'].max() ix可以查找行与列,但要用中括号实现,第一个中括号是行,第二个中括号是列,idxmax找的是最大的索引
print(movie.ix[[movie['Metascore'].idxmax()],['Title']])
标签:数据分析,Revenue,movie,如下,Director,max,print,解析,Millions 来源: https://blog.csdn.net/weixin_43788061/article/details/88594073