Python:彩票数据分析!

昨天,我们用Python爬取了双色球彩票的开奖信息,并将开奖号码存储在Excel中。今天,我们对开奖号码进行分析,看看哪些号码开出来的多,哪些开出来的少?

具体代码如下:

Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python。

#此程序用来分析双色球彩票的开奖数据信息
from xlrd import open_workbook
import matplotlib.pyplot as plt
import numpy as np
#打开Excel文件
rb = open_workbook('shuangseqiu.xls')
sheet = rb.sheet_by_index(0)
#将数据区分来看,取出红球和篮球数
reds=[]
blues=[]
for i in range(sheet.nrows - 1):
    reds.append(sheet.cell(i+1,2).value[:-2])
    blues.append(sheet.cell(i+1,2).value[-2:])
#画出红色球的柱形图
#plt.figure(figsize=(16, 12), dpi=100)
#plt.subplot(1, 2, 1)
plt.figure(1)
re1=[]
#统计处理红球信息
for i in reds:
    for j in range(1,7):
        re1.append(i[(j-1)*2:j*2])
#print(re1)
red01 = set(re1)
dict01 = {}
for item in red01:
    dict01.update({int(item): re1.count(item)})
#sorted(dict01.items(),key=lambda x:x[0])
print('The redball totalnumber is:\n ',sorted(dict01.items(),key=lambda x:x[1],reverse=True))
plt.bar(dict01.keys(),dict01.values(),0.5,color='r')
plt.xticks(np.arange(0,34,1))
for a,b in zip(dict01.keys(),dict01.values()):
    plt.text(a,b,b,ha='center',va='bottom',fontsize=10)
#plt.legend()
plt.xlabel('The ball Number')
plt.ylabel('Total Number')
plt.title("The Red ball's statistics")
plt.savefig('Redball.png')
plt.show()
#画出蓝色球的柱形图
plt.figure(2)
#统计处理篮球信息
bu01 = set(blues)
dict02 = {}
for item in bu01:
    dict02.update({int(item): blues.count(item)})
print('The blueball totalnumber is:\n ',sorted(dict02.items(),key=lambda x:x[1],reverse=True))
plt.bar(dict02.keys(),dict02.values(),0.75)
plt.xticks(np.arange(0,17,1))
for a,b in zip(dict02.keys(),dict02.values()):
    plt.text(a,b,b,ha='center',va='bottom',fontsize=10)
#plt.legend()
plt.xlabel('The ball Number')
plt.ylabel('Total Number')
plt.title("The Blue ball's statistics")
plt.savefig('Blueball.png')
plt.show()

运行结果如下:

1.直观的看一下:

2.统计分析:

The redball totalnumber is:

 [(1, 474), (14, 472), (22, 469), (26, 468), (20, 465), (18, 460), (8, 457), (32, 457), (7, 454), (17, 451), (6, 451), (13, 445), (3, 443), (10, 442), (19, 441), (5, 440), (25, 439), (27, 439), (30, 438), (2, 436), (12, 432), (4, 431), (16, 431), (11, 430), (23, 429), (21, 428), (9, 425), (29, 419), (31, 414), (15, 413), (28, 410), (24, 395), (33, 374)]

The blueball totalnumber is:

 [(12, 167), (9, 164), (16, 163), (11, 161), (14, 157), (7, 156), (1, 151), (13, 150), (5, 150), (15, 150), (6, 148), (3, 145), (10, 144), (2, 141), (4, 137), (8, 128)]

红球:“1”出现的次数最多,“33”出现的次数最少

篮球:“12”出现的次数最多,“8”出现的次数最少

好了,See you!

猜你喜欢

转载自blog.csdn.net/weixin_39108368/article/details/90142228