我正在使用IMDB数据开展个人项目,目前已经用尽所有途径。
快速概述:
到目前为止,我已执行以下操作:
plt.subplot2grid((2,3),(0,1))
actor_1 = df.pivot_table(index="Actor_1", values="Gross_Earnings", aggfunc='sum').sort_values(ascending=False)
actor_1[:15].sort_values(ascending=True).plot(kind='barh', width=0.7, alpha=0.5, color='red')
ax.tick_params(axis='x', labelsize=60)
ax.tick_params(axis='y', labelsize=60)
plt.xlabel("Gross Earnings")
plt.tight_layout()
plt.show()
这样可行,但它只返回最高值;不是顶部值与附加标准> 4部电影。
我也尝试了以下内容:
no_of_films = df.groupby("Actor_1")
name_count_key = df["Actor_1"].value_counts().to_dict()
no_of_films["Films"] = no_of_films["Actor_1"].map(name_count_key)
但它返回以下错误:" AttributeError:无法访问可调用属性' map' ' SeriesGroupBy'对象,尝试使用' apply'方法"
no_of_films = df.groupby("Actor_1")
name_count_key = df["Actor_1"].value_counts().to_dict()
no_of_films["Films"] = no_of_films["Actor_1"].apply(name_count_key)
但它返回以下错误:" TypeError:unhashable type:' dict'"
分组功能的想法是创建一个名为" Films"所以要计算每个演员出演的电影的音量,然后使用> 4但它返回bool而不是实际值。
Director Actor_1 IMDB_Score Gross_Earnings Movie_Title
Andrew Stanton Daryl Sabara 6.6 73058679 John Carter
Sam Raimi J.K. Simmons 6.2 336530303 Spider-Man 3
Nathan Greno Brad Garrett 7.8 200807262 Tangled
Joss Whedon Chris Hemsworth 7.5 458991599 Avengers: Age of Ultron
这可能还是我很傻?
非常感谢任何帮助。
谢谢,
阿德里安
答案 0 :(得分:1)
我认为您需要filter或boolean indexing
transform
:
print (df)
Director Actor_1 IMDB_Score Gross_Earnings Movie_Title
0 James Cameron CCH Pounder 7.9 760505847 Avatar
1 James Cameron CCH Pounder 7.9 760505847 Avatar1
2 James Cameron CCH Pounder 7.9 760505847 Avatar2
3 James Cameron CCH Pounder 7.9 760505847 Avatar3
4 Gore Verbinski Johnny Depp 7.1 309404152 Pirates
5 Sam Mendes Christoph Waltz 6.8 200074175 Spectre
6 Gore Verbinski Johnny Depp 7.1 309404152 Pirates1
7 Sam Mendes Christoph Waltz 6.8 200074175 Spectre1
8 Christopher Nolan Tom Hardy 8.5 448130642 The
df1 = df.groupby(["Actor_1"]).filter(lambda x: len(x) > 3)
print (df1)
Director Actor_1 IMDB_Score Gross_Earnings Movie_Title
0 James Cameron CCH Pounder 7.9 760505847 Avatar
1 James Cameron CCH Pounder 7.9 760505847 Avatar1
2 James Cameron CCH Pounder 7.9 760505847 Avatar2
3 James Cameron CCH Pounder 7.9 760505847 Avatar3
或更快的解决方案:
nofilms = df.groupby(["Actor_1"])['Movie_Title'].transform('size')
df1 = df[nofilms > 3]
print (df1)
Director Actor_1 IMDB_Score Gross_Earnings Movie_Title
0 James Cameron CCH Pounder 7.9 760505847 Avatar
1 James Cameron CCH Pounder 7.9 760505847 Avatar1
2 James Cameron CCH Pounder 7.9 760505847 Avatar2
3 James Cameron CCH Pounder 7.9 760505847 Avatar3
df2 = df1.groupby('Actor_1')['Gross_Earnings'].mean()
print (df2)
Actor_1
CCH Pounder 760505847
Name: Gross_Earnings, dtype: int64
最后一个情节是Series.plot.barh
:
df2.plot.barh()