1在genres类型中用‘’符合隔开2将年份作为索引将数据集按照年份分组得出每个年份各电影类型的数量3取2000-2016电影类型数量绘制热力图如下注:数据集里面有年份但不是索引列要求热力图的横坐标是年份。纵坐标是电影类型标题是电影热力图请给出代码
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt
读取数据集
df = pd.read_csv('movie.csv')
将genres类型中用‘|’符合隔开
df['genres'] = df['genres'].str.split('|')
将年份作为索引,将数据集按照年份分组,得出每个年份各电影类型的数量
df_year = df.set_index('year')['genres'].apply(pd.Series).stack().reset_index(level=1, drop=True).to_frame('genre') df_year = pd.get_dummies(df_year['genre']).join(df_year['year']).groupby('year').sum()
取2000-2016电影类型数量
df_year = df_year.loc[2000:2016]
绘制热力图
plt.figure(figsize=(12, 8)) sns.heatmap(df_year, cmap='YlGnBu') plt.title('电影热力图') plt.xlabel('电影类型') plt.ylabel('年份') plt.show(
原文地址: https://www.cveoy.top/t/topic/fbjv 著作权归作者所有。请勿转载和采集!