Python数据分析案例58——热门游戏数据分析及其可视化

案例背景

有哪个男生不喜欢玩游戏呢?就算上了班儿也要研究一下游戏以及热门的游戏。正好这里有个热门的游戏数据集,全球热门游戏数据集来做一下一些可视化的分析。


数据介绍

该文件包含一个数据集,详细说明了多个平台上的各种流行游戏。每个条目都包含重要信息,例如游戏名称、类型、平台、发布年份和用户评级。该数据集的结构便于分析和比较不同游戏,提供对游戏趋势和用户偏好的洞察。它采用CSV文件格式,以实现可访问性和易用性,使其适用于游戏行业的数据分析、可视化和研究目的。

热门的什么英雄联盟元神。还有国外的塞尔达,apex什么的都有。

需要这个数据集和这篇文章的全部代码文件的同学可以参考:游戏数据


代码实现

导入数据分析常用的包。展示数据的前五行

python 复制代码
# Import necessary libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Load the dataset
df_games = pd.read_csv('games_dataset.csv')

# Display the first few rows and the columns of the dataset
df_games.head()

简单的描述性统计

python 复制代码
# Display summary statistics of the dataset
df_games.describe(include='all')

查看数据是否有缺失值

python 复制代码
# Check for missing values
df_games.isnull().sum()

查看数据的类别

python 复制代码
# Remove any rows with missing values
df_games_clean = df_games.dropna()

# Ensure 'Release Year' is an integer
df_games_clean['Release Year'] = df_games_clean['Release Year'].astype(int)

# Check data types
df_games_clean.dtypes

可视化分析

游戏数量柱状图

展示数据里面前10数量的游戏的中文名称及他们的数量的微型柱状图。

python 复制代码
games_dict = {
    "The Legend of Zelda: Breath of the Wild": "塞尔达传说:荒野之息",
    "Genshin Impact": "原神",
    "The Sims 4": "模拟人生4",
    "Resident Evil Village": "生化危机村庄",
    "Hades": "哈迪斯",
    "Minecraft Dungeons": "我的世界:地下城",
    "Sekiro: Shadows Die Twice": "只狼:影逝二度",
    "Horizon Zero Dawn": "地平线:黎明时分",
    "Sea of Thieves": "盗贼之海",
    "Apex Legends": " apex 英雄"
}
df_games['Game Name'].value_counts().rename_axis('Game_Name').reset_index().iloc[:10,:]\
.assign(中文名称=lambda x:x['Game_Name'].map(games_dict)).set_axis(['Game Name','count','中文名称'],axis=1).style.bar()

可以看到这个游戏数据里面记录比较多的是塞尔达,荒野之息,这就是原神模拟人生等游戏。


整体用户评分直方图

python 复制代码
# Plot the distribution of user ratings
plt.figure(figsize=(10, 6))
sns.histplot(df_games_clean['User Rating'], bins=20, color='blue', edgecolor='black', kde=True)
plt.title('Distribution of User Ratings')
plt.xlabel('User Rating')
plt.ylabel('Frequency')
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

可以看到用户的评分较为均匀,从0分到十分,没有什么很突出的位置。说明游戏的评分没有很集中的区间。

游戏类型数量柱状图

python 复制代码
# Plot the distribution of genres
plt.figure(figsize=(12, 8))
sns.countplot(data=df_games_clean, y='Genre', order=df_games_clean['Genre'].value_counts().index, palette='viridis')
plt.title('Genre Distribution')
plt.xlabel('Count')
plt.ylabel('Genre')
plt.grid(axis='x', linestyle='--', alpha=0.7)
plt.show()

可以看到运动类的游戏排第一,后面其次都是各种类型的游戏。但是他们的数量差别都不会很大,说明各种游戏都是有自己的市场的。

不同游戏平台数量直方图

python 复制代码
# Plot the distribution of platforms
plt.figure(figsize=(12, 8))
sns.countplot(data=df_games_clean, y='Platform', order=df_games_clean['Platform'].value_counts().index, palette='magma')
plt.title('Platform Distribution')
plt.xlabel('Count')
plt.ylabel('Platform')
plt.grid(axis='x', linestyle='--', alpha=0.7)
plt.show()

PC端的游戏数量还是第一位,及个人电脑端,后面才是Xbox ,switch,以及移动端的各种。PS最少。

不同年份发行游戏数量的直方图

python 复制代码
# Plot the distribution of release years
plt.figure(figsize=(12, 8))
sns.histplot(df_games_clean['Release Year'], bins=20, color='green', edgecolor='black', kde=True)
plt.title('Distribution of Release Years')
plt.xlabel('Release Year')
plt.ylabel('Frequency')
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

我们可以看到大概在2000年,2007年,2015年以及2022年发行的游戏数量较多,其他年份都较为平均。


不同发行年份不同类型游戏用户评分散点图

python 复制代码
# Scatter plot of User Rating vs Release Year
plt.figure(figsize=(12, 8))
sns.scatterplot(data=df_games_clean, x='Release Year', y='User Rating', hue='Genre', palette='Set1', alpha=0.7)
plt.title('User Rating vs Release Year')
plt.xlabel('Release Year')
plt.ylabel('User Rating')
plt.grid(True)
plt.show()

可以看到不同年份的,不同类型的数据,他们的用户得分德还是较为均匀,不同类别之间这个图不好对比。很多年份数据分布很集中,都是很均匀,从0~10分的游戏都有。说明每一年都有很多好游戏和坏游戏,没有哪一年的游戏。质量特别高,也没有哪一年的游戏质量平均特别差。


不同游戏类型数量比例柱饼图

python 复制代码
# Pie chart of Genre Distribution
plt.figure(figsize=(10, 10))
genre_counts = df_games_clean['Genre'].value_counts()
plt.pie(genre_counts, labels=genre_counts.index, autopct='%1.1f%%', colors=sns.color_palette('pastel'), startangle=140)
plt.title('Genre Distribution')
plt.show()

和前面的结论一致,可以看到游戏类型最多的是运动类。而其他的类别分布都没有差太远的比例,说明各种游戏都有各自的市场,没有哪一种游戏是极少或者极多的。

不同游戏平台数量比例柱饼图

python 复制代码
# Pie chart of Platform Distribution
plt.figure(figsize=(10, 10))
platform_counts = df_games_clean['Platform'].value_counts()
plt.pie(platform_counts, labels=platform_counts.index, autopct='%1.1f%%', colors=sns.color_palette('pastel'), startangle=140)
plt.title('Platform Distribution')
plt.show()

和前面结论一致,PC端的数量占比最高,说明目前大家还是用电脑玩游戏的最多,当然其他的平台差距也不大,各个平台都有各自的用户。


不同类别的游戏用户评分箱线图

python 复制代码
# Boxplot of User Ratings by Genre
plt.figure(figsize=(14, 8))
sns.boxplot(data=df_games_clean, x='Genre', y='User Rating', palette='coolwarm')
plt.title('User Ratings by Genre')
plt.xlabel('Genre')
plt.ylabel('User Rating')
plt.xticks(rotation=45)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

可以从均值的角度来看,发现生存类的游戏评分均值最高。其次是恐怖游戏和角色扮演类动作类游戏。但是他们整体来说均值差异都不大。


不同平台用户评分箱线图

python 复制代码
# Boxplot of User Ratings by Platform
plt.figure(figsize=(14, 8))
sns.boxplot(data=df_games_clean, x='Platform', y='User Rating', palette='coolwarm')
plt.title('User Ratings by Platform')
plt.xlabel('Platform')
plt.ylabel('User Rating')
plt.xticks(rotation=45)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

我们可以看到PC端的用户评分均值最高,其次是其他4个都差不多。并且大家的分布都很均匀,差距都不是很大。


不同类型游戏用户评分均值柱状图

python 复制代码
# Calculate average user rating by genre
avg_rating_by_genre = df_games_clean.groupby('Genre')['User Rating'].mean().sort_values()

# Plot average user ratings by genre
plt.figure(figsize=(12, 8))
avg_rating_by_genre.plot(kind='barh', color='purple', edgecolor='black')
plt.title('Average User Ratings by Genre')
plt.xlabel('Average User Rating')
plt.ylabel('Genre')
plt.grid(axis='x', linestyle='--', alpha=0.7)
plt.show()

和前面的一致,生存类,恐怖类,模拟经营类的动作类运动类。评分前五,其他的游戏评分略低,但是也还好。差距都不是很大。

不同平台用户评分均值柱状图

python 复制代码
# Calculate average user rating by platform
avg_rating_by_platform = df_games_clean.groupby('Platform')['User Rating'].mean().sort_values()

# Plot average user ratings by platform
plt.figure(figsize=(12, 8))
avg_rating_by_platform.plot(kind='barh', color='orange', edgecolor='black')
plt.title('Average User Ratings by Platform')
plt.xlabel('Average User Rating')
plt.ylabel('Platform')
plt.grid(axis='x', linestyle='--', alpha=0.7)
plt.show()

和前面结论一致pc端电脑端的用户评分均值最高,其他的都差不多。


发行年份和用户评分的相关性热力图

python 复制代码
# Compute correlation matrix
corr_matrix = df_games_clean[['Release Year', 'User Rating']].corr()

# Plot correlation heatmap
plt.figure(figsize=(8, 6))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt='.2f', linewidths=0.5)
plt.title('Correlation Heatmap')
plt.show()

可以看到和散点图的结论一样,发行年份和评分没有相关性。就是零。

游戏类别和游戏平台的评分相关系数热力图

python 复制代码
# Pivot table for heatmap
pivot_table = df_games_clean.pivot_table(values='User Rating', index='Genre', columns='Platform', aggfunc='mean')

# Plot heatmap
plt.figure(figsize=(14, 8))
sns.heatmap(pivot_table, cmap='YlGnBu', annot=True, fmt='.1f', linewidths=0.5)
plt.title('Heatmap of User Ratings by Genre and Platform')
plt.xlabel('Platform')
plt.ylabel('Genre')
plt.show()

我们可以看到某些平台的某些类型的游戏评分明显要高于其他,例如电脑端的策略类和生存类,还有格斗类以及射击类明显要优于其他平台,也要优于电脑端的其他的类型的游戏的评分的均值。


python 复制代码
# Plot the count of games by Release Year and Genre
plt.figure(figsize=(14, 8))
sns.countplot(data=df_games_clean, x='Release Year', hue='Genre', palette='Set2')
plt.title('Distribution of Games by Release Year and Genre')
plt.xlabel('Release Year')
plt.ylabel('Number of Games')
plt.xticks(rotation=45)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

这个是不同年份发行不同类别的游戏,难以看出明显的趋势,只能大概知道2006年发行的Puzzle这个类型的游戏比较多。


不同年份发行游戏数量折线图

python 复制代码
# Plot trends in the number of releases per year
plt.figure(figsize=(14, 8))
release_year_trends = df_games_clean['Release Year'].value_counts().sort_index()
release_year_trends.plot(kind='line', marker='o', color='tomato')
plt.title('Trends in Number of Game Releases Per Year')
plt.xlabel('Release Year')
plt.ylabel('Number of Releases')
plt.grid(True)
plt.show()

可以清楚的看到每个年份发行的游戏的数量的一个变化趋势。


不同类型的游戏的用户评分的核密度图

python 复制代码
# Kernel Density Estimate (KDE) plot of user ratings by genre
plt.figure(figsize=(14, 8))
sns.kdeplot(data=df_games_clean, x='User Rating', hue='Genre', common_norm=False, palette='tab10')
plt.title('Density Plot of User Ratings by Genre')
plt.xlabel('User Rating')
plt.ylabel('Density')
plt.grid(True)
plt.show()

其实和前面结论一样,不同类型的这些用户评分分布都挺相近的,可能有的稍微高一点点,但是也不会高很多。并且分布都是两端比较少,也就是说很好或者很差的游戏比较少,大多数游戏都集中在中间的2-9分的区间。


不同平台发布不同类型游戏的数量热力图

python 复制代码
# Create a DataFrame for analysis of platform and genre pairs
platform_genre_counts = df_games_clean.groupby(['Platform', 'Genre']).size().unstack().fillna(0)

# Plot
plt.figure(figsize=(16, 10))
sns.heatmap(platform_genre_counts, cmap='viridis', annot=True, fmt='g', linewidths=0.5)
plt.title('Platform and Genre Pair Analysis')
plt.xlabel('Genre')
plt.ylabel('Platform')
plt.show()

可以从图中清楚的看到每个平台发布每个不同类型的游戏的数量。例如运动类在移动端和电脑端发布的数量是最多的,其次就是xbox上的角色扮演类的游戏数量比较多。

不同发布年份用户评分的箱线图

python 复制代码
# Box plot of user ratings by release year
plt.figure(figsize=(14, 8))
sns.boxplot(data=df_games_clean, x='Release Year', y='User Rating', palette='coolwarm')
plt.title('User Ratings by Release Year')
plt.xlabel('Release Year')
plt.ylabel('User Rating')
plt.xticks(rotation=45)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

可以看到大概在2004年和2016年发布的游戏评分均值较高,2007年和2023年发布的游戏评分均值较低,但整体变化波动幅度不是很大。


不同发布年份用户评分均值的折线图

python 复制代码
# Line plot of average user ratings over time (by Release Year)
plt.figure(figsize=(14, 8))
avg_rating_over_time = df_games_clean.groupby('Release Year')['User Rating'].mean()
avg_rating_over_time.plot(kind='line', marker='o', color='skyblue')
plt.title('Average User Ratings Over Time')
plt.xlabel('Release Year')
plt.ylabel('Average User Rating')
plt.grid(True)
plt.show()

不同年份用户评分均值折线图。

可以清楚地看到不同年份的游戏数据评分均值分布。和前面的箱线图得到的结论也是类似的。


后记

只是简单的画了一些柱状图,折线图。直方图密度图,其实还有很多箱线图。概率密度图,小提琴图,点图以及散点,面积,气泡,雷达等图都可以画,但是目前简单的分析就这个样子。

其实还可以单独抽出各种游戏,例如原神整体的每个年份的一个评分的变化分布之类的,但是我没有很偏好的单独的游戏,所以就没有做,大家有兴趣可以自己做一下。

数据和全部代码文件获取参考:游戏数据


创作不易,看官觉得写得还不错的话点个关注和赞吧,本人会持续更新python数据分析领域的代码文章~(需要定制类似的代码可私信)

相关推荐
这里有鱼汤1 小时前
【花姐小课堂】新手也能秒懂!用「风险平价」打造扛造的投资组合
后端·python
databook15 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar16 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805116 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_16 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
RestCloud16 小时前
数据传输中的三大难题,ETL 平台是如何解决的?
数据分析·api
数据智能老司机1 天前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机1 天前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——性能模式
python·设计模式·架构