Python數(shù)據(jù)分析之雙色球統(tǒng)計兩個紅和藍(lán)球哪組合比例高的方法
本文實例講述了Python數(shù)據(jù)分析之雙色球統(tǒng)計兩個紅和藍(lán)球哪組合比例高的方法。分享給大家供大家參考,具體如下:
統(tǒng)計兩個紅球和藍(lán)球,哪個組合最多,顯示前19組數(shù)據(jù)
#!/usr/bin/python
# -*- coding:UTF-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import operator
#導(dǎo)入數(shù)據(jù)
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
#藍(lán)球
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
#將藍(lán)球添加到各紅球組中(有2列數(shù)據(jù)變?yōu)?列數(shù)據(jù)),之后將所有數(shù)據(jù)按列向合并
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)
#讀取文件,進(jìn)行統(tǒng)計,并且從大倒小排序
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替換成數(shù)值,便于畫圖使用
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的內(nèi)容顯示出來
plt.xticks(s,x, rotation=30,size=10,ha='left')
plt.show()
顯示結(jié)果:

可以看出紅球20、26和藍(lán)球9以及紅球17、21和藍(lán)球14,出現(xiàn)次數(shù)最多12次
后期的3紅球和藍(lán)球,4紅球和藍(lán)球,5紅球和藍(lán)球,6紅球和藍(lán)球的統(tǒng)計,基本思路一致。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運算技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
詳解python和matlab的優(yōu)勢與區(qū)別
在本文中小編給大家分享的是關(guān)于python和matlab的優(yōu)勢與區(qū)別的知識點以及實例代碼,需要的朋友們參考學(xué)習(xí)下。2019-06-06
python發(fā)qq消息轟炸虐狗好友思路詳解(完整代碼)
因為我的某個好友在情人節(jié)的時候秀恩愛,所以我靈光一閃制作了qq消息轟炸并記錄了下來。本文給大家分享python發(fā)qq消息轟炸虐狗好友思路詳解,感興趣的朋友一起看看吧2020-02-02

