python数据分析4:双色球 两个红和蓝球哪组合比例高

来源:互联网 发布:炉石盒子 mac 编辑:程序博客网 时间:2024/05/24 23:13

统计两个红球和蓝球,哪个组合最多,显示前19组数据

#!/usr/bin/python# -*- coding:UTF-8 -*-import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport operator#导入数据df = pd.read_table('newdata.txt',header=None,sep=',')tdate = sorted(df.loc[:,0])# print tdate#第1、2列的红球h1 = df.loc[:,1:2].values# print h1#第2、3列的红球h2 = df.loc[:,2:3].values#第3、4列的红球h3 = df.loc[:,3:4].values#第4、5列的红球h4 = df.loc[:,4:5].values#第5、6列的红球h5 = df.loc[:,5:6].values#蓝球b1 = df.loc[:,7:7].values# print b1#第1、3列红球h6 = df.loc[:,1:3:2].valuesh7 = df.loc[:,1:4:3].valuesh8 = df.loc[:,1:5:4].valuesh9 = df.loc[:,1:6:5].valuesh10 = df.loc[:,2:4:2].valuesh11 = df.loc[:,2:5:3].valuesh12 = df.loc[:,2:6:4].valuesh13 = df.loc[:,3:5:2].valuesh14 = df.loc[:,3:6:3].values#第4、6列红球h15 = df.loc[:,4:6:2].values#将蓝球添加到各红球组中(有2列数据变为3列数据),之后将所有数据按列向合并data2 = np.append(h1, b1, axis=1)for i in [h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15]:    data1 = np.append(i, b1, axis=1)    data2 = np.append(data2, data1, axis=0)print data2data1 = pd.DataFrame(data2)#写入到2hldata.csv文件中data1.to_csv('2hldata.csv',index=None,header=None)#读取文件,进行统计,并且从大倒小排序f = open("2hldata.csv")count_dict = {}for line in f.readlines():    line = line.strip()    count = count_dict.setdefault(line, 0)    count += 1    count_dict[line] = countsorted_count_dict = sorted(count_dict.iteritems(), key=operator.itemgetter(1), reverse=True)# for item in sorted_count_dict:#     print "%s,%d" % (item[0], item[1])#重置DataFrame的indexfenzu = pd.DataFrame(sorted_count_dict).set_index([0])print fenzux = list(fenzu.index[:19])y = list(fenzu.values[:19])print xprint y#将index替换成数值,便于画图使用s = pd.Series(range(1,len(x)+1), index=x)plt.figure(figsize=(12,8),dpi=80)plt.legend(loc='best')plt.bar(s,y,alpha=.5, color='r',width=0.8)plt.title('The two red and one blue ball number')plt.xlabel('two red and one blue number')plt.ylabel('times')#将原来index的内容显示出来plt.xticks(s,x, rotation=30,size=10,ha='left')plt.show()

显示结果:

可以看出红球20、26和蓝球9以及红球17、21和蓝球14,出现次数最多12次

后期的3红球和蓝球,4红球和蓝球,5红球和蓝球,6红球和蓝球的统计,基本思路一致。

0 0
原创粉丝点击