Python數(shù)據(jù)分析之雙色球統(tǒng)計單個紅和藍球哪個比例高的方法
本文實例講述了Python數(shù)據(jù)分析之雙色球統(tǒng)計單個紅和藍球哪個比例高的方法。分享給大家供大家參考,具體如下:
統(tǒng)計單個紅球和藍球,哪個組合最多,顯示前19組數(shù)據(jù)
#!/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 h1 = df.loc[:,1:7:6].values #取第一列紅球和藍球 # print h1 h2 = df.loc[:,2:7:5].values #取第二列紅球和藍球 h3 = df.loc[:,3:7:4].values h4 = df.loc[:,4:7:3].values h5 = df.loc[:,5:7:2].values h6 = df.loc[:,6:7:1].values # tblue = df.loc[:,7] #將上方切分的所有數(shù)據(jù)組合到一起 data = np.append(h1, h2, axis = 0) data = np.append(data, h3, axis = 0) data = np.append(data, h4, axis = 0) data = np.append(data, h5, axis = 0) data = np.append(data, h6, axis = 0) # print data data1 = pd.DataFrame(data) # print data1 #寫入到一個文件中 data1.to_csv('hldata.csv',index=None,header=None) #讀取文件,將組合進行統(tǒng)計并從大到小排序 f = open("hldata.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]) # print sorted_count_dict fenzu = pd.DataFrame(sorted_count_dict).set_index([0]) #print fenzu #分別從第一列和第二列取前19個數(shù)據(jù)放到x y中 x = list(fenzu.index[:19]) y = list(fenzu.values[:19]) print x print y #將x對應(yīng)數(shù)值,不然畫圖報錯 s = pd.Series(range(1,len(x)+1), index=x) #設(shè)置畫圖屬性 plt.figure(figsize=(12,6),dpi=80) plt.legend(loc='best') # plt.plot(fenzu,color='red') plt.bar(s,y,alpha=.5, color='r',width=0.8) plt.title('The one red and one blue ball number') plt.xlabel('one red and one blue 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') #將['1,12', '26,9', '5,13']這樣的字符放到圖中 plt.xticks(s,x, rotation=10,size=10,ha='left') plt.show()
結(jié)果如下:
可以看出紅球1和藍球12出現(xiàn)過的次數(shù)最多,其次是紅球26和藍球9
參考:
import matplotlib.pyplot as plt import numpy as np plt.rc('font', family='SimHei', size=13) num = np.array([13325, 9403, 9227, 8651]) ratio = np.array([0.75, 0.76, 0.72, 0.75]) men = num * ratio women = num * (1-ratio) x = ['聊天','支付','團購\n優(yōu)惠券','在線視頻'] width = 0.5 idx = np.arange(len(x)) plt.bar(idx, men, width, color='red', label='男性用戶') plt.bar(idx, women, width, bottom=men, color='yellow', label='女性用戶') plt.xlabel('應(yīng)用類別') plt.ylabel('男女分布') plt.xticks(idx+width/2, x, rotation=40) plt.legend()
更多關(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īng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- 基于Python數(shù)據(jù)分析之pandas統(tǒng)計分析
- python實現(xiàn)的分析并統(tǒng)計nginx日志數(shù)據(jù)功能示例
- Python數(shù)據(jù)可視化 pyecharts實現(xiàn)各種統(tǒng)計圖表過程詳解
- Python實現(xiàn)Mysql數(shù)據(jù)統(tǒng)計及numpy統(tǒng)計函數(shù)
- python-itchat 統(tǒng)計微信群、好友數(shù)量,及原始消息數(shù)據(jù)的實例
- Python如何獲得百度統(tǒng)計API的數(shù)據(jù)并發(fā)送郵件示例代碼
- Python數(shù)據(jù)分析之雙色球統(tǒng)計兩個紅和藍球哪組合比例高的方法
- Python數(shù)據(jù)分析之雙色球中藍紅球分析統(tǒng)計示例
- Python實現(xiàn)讀寫sqlite3數(shù)據(jù)庫并將統(tǒng)計數(shù)據(jù)寫入Excel的方法示例
- 用python實現(xiàn)簡單EXCEL數(shù)據(jù)統(tǒng)計的實例
- Python 數(shù)據(jù)的累加與統(tǒng)計的示例代碼
相關(guān)文章
python3中No module named _ssl的問題解決
本文主要介紹了python3中No module named _ssl的問題解決,這個錯誤表示Python導(dǎo)入_ssl模塊時失敗,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08Python內(nèi)置函數(shù)—vars的具體使用方法
本篇文章主要介紹了Python內(nèi)置函數(shù)—vars的具體使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決
這篇文章主要為大家介紹了python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09Python實現(xiàn)抓取網(wǎng)頁并且解析的實例
這篇文章主要介紹了Python實現(xiàn)抓取網(wǎng)頁并且解析的功能實例,主要以解析百度問答為例說明其原理與方法,需要的朋友可以參考下2014-09-09AI生成圖片Stable?Diffusion環(huán)境搭建與運行方法
Stable?Diffusion是一種基于擴散過程的生成模型,由Ge?et?al.在2021年提出,該模型利用了隨機變量的穩(wěn)定分布,通過遞歸地應(yīng)用擴散過程來生成高質(zhì)量的圖像,這篇文章主要介紹了AI圖片生成Stable?Diffusion環(huán)境搭建與運行,需要的朋友可以參考下2023-05-05