栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python数据分析之双色球统计两个红和蓝球哪组合比例高的方法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python数据分析之双色球统计两个红和蓝球哪组合比例高的方法

本文实例讲述了Python数据分析之双色球统计两个红和蓝球哪组合比例高的方法。分享给大家供大家参考,具体如下:

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

#!/usr/bin/python
# -*- coding:UTF-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import 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].values
h7 = df.loc[:,1:4:3].values
h8 = df.loc[:,1:5:4].values
h9 = df.loc[:,1:6:5].values
h10 = df.loc[:,2:4:2].values
h11 = df.loc[:,2:5:3].values
h12 = df.loc[:,2:6:4].values
h13 = df.loc[:,3:5:2].values
h14 = 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 data2
data1 = 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] = count
sorted_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的index
fenzu = pd.Dataframe(sorted_count_dict).set_index([0])
print fenzu
x = list(fenzu.index[:19])
y = list(fenzu.values[:19])
print x
print 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红球和蓝球的统计,基本思路一致。

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/31197.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号