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

来源:互联网 发布:用java会议管理系统 编辑:程序博客网 时间:2024/05/16 18:14

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

#!/usr/bin/python# -*- coding:UTF-8 -*-#导入pandas、numpy、matplotlib、operator包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])#截取红球,2个红球组合方式h1 = df.loc[:,1:2].values# print h1h2 = df.loc[:,2:3].valuesh3 = df.loc[:,3:4].valuesh4 = df.loc[:,4:5].valuesh5 = df.loc[:,5:6].values# b1 = df.loc[:,7:7].values# print b1h6 = 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].valuesh15 = df.loc[:,4:6:2].values# data2 = np.append(h1, b1, axis=1)#将截取的2个红球,组合到一起data2 = h1for i in [h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15]:    data2 = np.append(data2, i, axis=0)print len(data2)#数据放到DataFramedata1 = pd.DataFrame(data2)# print data1#输出数据到文件data1.to_csv('2hdata.csv',index=None,header=None)#以字典方式将数据进行统计,并从大倒小排序f = open("2hdata.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])# print sorted_count_dict#重置DataFrame的index  fenzu = 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 ball number')plt.xlabel('two red number')plt.ylabel('times')# for i in  range(0,19):#     plt.text(int(i+1.4),25,x[i],color='b',size=10)# plt.text(1.4,20,x[0],color='g',ha='center')#将原来index的内容显示出来  plt.xticks(s,x, rotation=30,size=10,ha='left')plt.show()

显示结果:

红球组合20、26出现过80次,3和5组合方式出现过79次,号码14和30出现过75次

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

0 0
原创粉丝点击