ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

基于python的游戏市场分析

2021-10-05 16:03:58  阅读:229  来源: 互联网

标签:基于 plt 游戏 python sum Sales sale df 营收


 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#读取文件
df=pd.DataFrame(pd.read_csv('vgsales.csv'))

#初步了解
print(df.info())
print(df.describe())

#检查空值
print(df.isnull().any())
print(df.isnull().sum())
1.不同体裁的游戏在营收方面的表现

(纵向对比,即比较不同的平台在同一方面的营收情况)

genre_sale=df.groupby('Genre').agg(NAsales=('NA_Sales','sum'),EUsales=('EU_Sales','sum'),
                                   JPsales=('JP_Sales','sum'),Othsales=('Other_Sales','sum'),
                                   Glosales=('Global_Sales','sum')).sort_values(by='Genre')

1.1全球销量排名
genre_sale['rank']=genre_sale['Glosales'].rank(method='min',ascending=False)
 

1.2全球销量排名前三的体裁
print(genre_sale.query('rank<=3'))

1.3绘图

1.3.1折线图

plt.figure()
x=genre_sale.index
y=genre_sale['Glosales']
z=genre_sale['Othsales']
plt.xticks(rotation=30)
plt.plot(x, y,label='glo')
plt.plot(x,z,label='oth')
plt.legend()
for a,b in zip(x,y):
    plt.text(a,b+10,'%.2f' % b,ha='center')
for a,b in zip(x,z):
    plt.text(a,b+10,'%.2f' % b,ha='center')  
    
 
plt.figure()
c=genre_sale['NAsales']
d=genre_sale['EUsales']
e=genre_sale['JPsales']
plt.plot(x,c,label='NA')
plt.plot(x,d,label='EU')
plt.plot(x,e,label='JP')
plt.legend()
plt.xticks(rotation=30)
 

1.3.2条形图(结构条形图)

plt.figure()
plt.bar(x,c,label='NA',color='pink')
plt.bar(x,d,label='EU',color='purple')
plt.bar(x,e,label='JP',color='lightblue')
plt.legend()
plt.xticks(rotation=30)
    
for a,b in zip(x,c):
    plt.text(a,b,'%.2f' % b,ha='center')
    
1.4评级

def sale_label(x):
    if x>np.mean(genre_sale['Glosales']):
        return 'high'
    else:
        return 'low'
    
genre_sale['label']=genre_sale['Glosales'].apply(lambda x:sale_label(x))

 

 

 

2.不同平台在营收方面的表现

plat_sale=df.groupby('Platform').agg(NAsales=('NA_Sales','sum'),EUsales=('EU_Sales','sum'),
                                   JPsales=('JP_Sales','sum'),Othsales=('Other_Sales','sum'),
                                   Glosales=('Global_Sales','sum'))

2.1纵向对比,即比较不同的平台在同一方面的营收情况

plat_sale['rank']=plat_sale['Glosales'].rank(method='min',ascending=False)

plt.figure()
for i in range(plat_sale.shape[1]):
    x1=plat_sale.index
    y1=plat_sale.iloc[:,i]
    plt.bar(x1,y1,color='orange')
    plt.xticks(rotation=90)
    plt.ylabel(plat_sale.columns[i])
    plt.show()

2.2横向对比,即比较同一平台在不同方面的营收情况

for i in range(len(plat_sale)):
    f1=pd.DataFrame(plat_sale.iloc[i,:])
    plt.plot(f1,color='orange')
    plt.ylabel(f1.columns)
    for n,g in zip(f1.index,f1.iloc[:,0]):
        plt.text(n,g,'%.2f' % g,ha='center')
    plt.show()

纵向 

 

 

 

 

横向 

 

3.不同出版商在营收方面的表现

3.1分组计算
pub_sale=df.groupby('Publisher').agg(NAsales=('NA_Sales','sum'),EUsales=('EU_Sales','sum'),
                                   JPsales=('JP_Sales','sum'),Othsales=('Other_Sales','sum'),
                                   Glosales=('Global_Sales','sum'))

3.2排名
pub_sale['rank']=pub_sale['Glosales'].rank(method='min',ascending=False)
pub_sale_20=pub_sale.query('rank<=20')

3.3画图
plt.figure()
x2=pub_sale_20.index
y2=pub_sale_20['Glosales']
plt.plot(x2,y2,color='orange')
plt.xticks(rotation=90)
for a,b in zip(x2,y2):
    plt.text(a,b+10,'%.2f' % b,ha='center')

 

 

 

4.不同年份在营收方面的的表现

4.1给年份分组

def year_group(x):
    if x<=1990:
        return '1980-1990'
    elif x<=2000:
        return '1991-2000'
    elif x<=2010:
        return '2001-2010'
    else:
        return '2011-2020'

list1=[]
for i in df['Year']:
    a=year_group(i)
    list1.append(a)   
df['year_group']=list1

  #另一种写法
  #df['year_group']=df['Year'].apply(lambda x:year_group(x))

4.2分组计算

year_sale=df.groupby('year_group').agg(NAsales=('NA_Sales','sum'),EUsales=('EU_Sales','sum'),
                                   JPsales=('JP_Sales','sum'),Othsales=('Other_Sales','sum'),
                                   Glosales=('Global_Sales','sum'))

4.3画图
plt.figure()
for i in range(year_sale.shape[1]):
    x2=year_sale.index
    y2=year_sale.iloc[:,i]
    plt.plot(x2,y2,label=year_sale.columns[i])
    for a,b in zip(x2,y2):
        plt.text(a,b,'%.2f' % b,ha='center')
    plt.legend()

 

5.不同游戏在营收方面的表现

5.1分组计算
game_sale=df.groupby('Name').agg(NAsales=('NA_Sales','sum'),EUsales=('EU_Sales','sum'),
                                   JPsales=('JP_Sales','sum'),Othsales=('Other_Sales','sum'),
                                   Glosales=('Global_Sales','sum'))

5.2游戏在不同国家营收的排名
for i in range(game_sale.shape[1]):
    coname=game_sale.columns
    game_sale[coname[i]+'_rate']=game_sale.iloc[:,i].rank(method='min',ascending=False)

5.3取出不同国家营收排名前十的游戏
for i in range(5,10):
    outn=game_sale[game_sale.iloc[:,i]<=10].iloc[:,i]
    print(outn)

标签:基于,plt,游戏,python,sum,Sales,sale,df,营收
来源: https://blog.csdn.net/weixin_49687178/article/details/120614092

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有