Python數(shù)據(jù)分析之雙色球統(tǒng)計(jì)兩個(gè)紅和藍(lán)球哪組合比例高的方法
本文實(shí)例講述了Python數(shù)據(jù)分析之雙色球統(tǒng)計(jì)兩個(gè)紅和藍(lán)球哪組合比例高的方法。分享給大家供大家參考,具體如下:
統(tǒng)計(jì)兩個(gè)紅球和藍(lán)球,哪個(gè)組合最多,顯示前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)計(jì),并且從大倒小排序 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)計(jì),基本思路一致。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
如何利用python實(shí)現(xiàn)把視頻轉(zhuǎn)換成gif圖形
將視頻轉(zhuǎn)換為 GIF 圖形的重要性不言而喻,在信息快速傳播和多種社交平臺(tái)廣泛應(yīng)用的背景下,GIF 動(dòng)畫不僅為個(gè)人用戶提供了一種輕松的表達(dá)方式,本文給大家介紹了如何利用python實(shí)現(xiàn)把視頻轉(zhuǎn)換成gif圖形,需要的朋友可以參考下2024-10-10Python多線程編程threading模塊使用最佳實(shí)踐及常見問題解析
這篇文章主要為大家介紹了Python多線程編程threading模塊使用最佳實(shí)踐及常見問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python內(nèi)建屬性getattribute攔截器使用詳解
這篇文章主要為大家介紹了Python內(nèi)建屬性getattribute攔截器使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05詳解python實(shí)現(xiàn)讀取郵件數(shù)據(jù)并下載附件的實(shí)例
這篇文章主要介紹了詳解python讀取郵件數(shù)據(jù)并下載附件的實(shí)例的相關(guān)資料,這里提供實(shí)現(xiàn)實(shí)例,幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08Python實(shí)戰(zhàn)購(gòu)物車項(xiàng)目的實(shí)現(xiàn)參考
今天小編就為大家分享一篇關(guān)于Python實(shí)戰(zhàn)購(gòu)物車項(xiàng)目的實(shí)現(xiàn)參考,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02python 用正則表達(dá)式篩選文本信息的實(shí)例
今天小編就為大家分享一篇python 用正則表達(dá)式篩選文本信息的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06