Pandas学习(二)——双色球开奖数据分析
学习笔记汇总
Pandas学习(一)–数据的导入
pandas学习(二)–双色球数据分析
pandas学习(三)–NAB球员薪资分析
pandas学习(四)–数据的归一化
pandas学习(五)–pandas学习视频
本章主要利用双色球开奖数据来学习pandas的DataFrame数据选取,Series的统计功能,以及matplotlib画柱状图。
ball.py
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from utils import *
HISTORY_NUM = "079" # 历史同期
HEAD_NUM = 30 # 近30期
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
# read data
data_sheet = 'ssqexcle_result.xls'
all_data = pd.read_excel(data_sheet, parse_cols=(0, 2, 3, 4, 5, 6, 7, 8))
all_data['index'] = all_data['index'].astype(np.str)
#统计历史数据
history_data = all_data[[x.endswith(HISTORY_NUM) for x in all_data['index']]].copy()
history_red_ball = history_data.iloc[:, 1:7]
history_blue_ball = history_data.iloc[:, 7]
count_red_ball = history_red_ball.stack().value_counts()
count_blue_ball = history_blue_ball.value_counts()
# 画图
plt.figure(1)
count_red_ball.plot(kind='bar', align='center')
plt.xlabel("红球数字")
plt.ylabel("次数")
plt.show()
plt.figure(2)
count_blue_ball.plot(kind='bar', align='center')
plt.xlabel("蓝球数字")
plt.ylabel("次数")
plt.show()
#统计近n期数据
head_n_data = all_data.head(HEAD_NUM).copy()
head_n_red_ball = head_n_data.iloc[:, 1:7]
head_n_blue_ball = head_n_data.iloc[:, 7]
odd_even_dict = calculate_odd_vs_even(head_n_red_ball) #计算奇偶比
print(odd_even_dict)
#计算3区比
result = cal_3distance_times(all_data.iloc[:, 1:7])
print(result)
# 统计红球2连号出现次数
result = cal_2sequence_num_times(all_data.iloc[:, 1:7])
print(result)
# 统计红球3连号出现次数
result = cal_3sequence_num_times(all_data.iloc[:, 1:7])
print(result)
#统计2个球组合出现的频次
result = cal_2combination_times(all_data.iloc[:, 1:7])
print(result)
#统计3个红球组合出现的频次
result = cal_3combination_times(all_data.iloc[:, 1:7])
print(result)
utils.py
# -*- coding: utf-8 -*-
def calculate_odd_vs_even(dataframe):
# 计算奇偶比
result_dict = {}
for index, row in dataframe.iterrows():
odd = 0 # 奇数
even = 0 # 偶数
for x in row:
if x & 1:
odd += 1
else:
even += 1
key = "%s:%s" % (str(odd), str(even))
if key in result_dict:
result_dict[key] += 1
else:
result_dict[key] = 1
return result_dict
def cal_2sequence_num_times(dataframe):
"""
计算红球2连号出现的频次
"""
result_dict = {}
for index, row in dataframe.iterrows():
a = 1
while a < 33:
if (a in row.values) and (a + 1 in row.values):
key = "%s:%s" % (str(a), str(a + 1))
if key in result_dict:
result_dict[key] += 1
else:
result_dict[key] = 1
a += 1
return sorted(result_dict.items(), key=lambda item: item[1])
def cal_3sequence_num_times(dataframe):
"""
计算红球3连号出现的频次
"""
result_dict = {}
for index, row in dataframe.iterrows():
a = 1
while a < 32:
if (a in row.values) and (a + 1 in row.values) and (a + 2 in row.values):
key = "%s:%s:%s" % (str(a), str(a + 1), str(a + 2))
if key in result_dict:
result_dict[key] += 1
else:
result_dict[key] = 1
a += 1
return sorted(result_dict.items(), key=lambda item: item[1])
def cal_3distance_times(dataframe):
"""
计算红球的3区比
"""
result_dict = {}
for index, row in dataframe.iterrows():
little = 0 # 小区
middle = 0 # 中区
big = 0 # 大区
for x in row.values:
if x <= 11:
little += 1
elif x <= 22:
middle += 1
else:
big += 1
key = "%s:%s:%s" % (str(little), str(middle), str(big))
if key in result_dict:
result_dict[key] += 1
else:
result_dict[key] = 1
return sorted(result_dict.items(), key=lambda item: item[1])
def cal_2combination_times(dataframe):
"""
计算红球2个球组合出现的频次
"""
result_dict = {}
for index, row in dataframe.iterrows():
for a in range(6):
for b in range(a + 1, 6):
key = "%s:%s" % (str(row.values[a]), str(row.values[b]))
if key in result_dict:
result_dict[key] += 1
else:
result_dict[key] = 1
return sorted(result_dict.items(), key=lambda item: item[1])
def cal_3combination_times(dataframe):
"""
计算红球3个球组合出现的频次
"""
result_dict = {}
for index, row in dataframe.iterrows():
for a in range(6):
for b in range(a + 1, 6):
for c in range(b + 1, 6):
key = "%s:%s:%s" % (str(row.values[a]), str(row.values[b]), str(row.values[c]))
if key in result_dict:
result_dict[key] += 1
else:
result_dict[key] = 1
return sorted(result_dict.items(), key=lambda item: item[1])
获取出来的历史数据如下所示:
index red1 red2 red3 red4 red5 red6 blue
20 2017079 3 7 14 23 25 27 08
173 2016079 1 3 10 12 24 28 02
327 2015079 9 14 15 20 26 32 11
479 2014079 2 7 16 22 27 28 02
633 2013079 7 13 17 19 22 26 13
787 2012079 6 7 12 24 30 33 12
940 2011079 3 14 15 16 24 29 05
1093 2010079 8 11 12 14 18 22 02
1247 2009079 2 9 16 21 30 31 13
1401 2008079 3 4 5 10 20 32 09
1554 2007079 3 4 14 20 21 25 14
1708 2006079 6 11 13 17 20 32 08
1861 2005079 3 9 20 24 25 28 05
1983 2004079 7 13 14 17 19 30 03
2072 2003079 12 15 22 23 26 31 04
画出来的 图如下所示:
1、红球历史数据次数统计图
2、蓝球历史数据次数统计图
数据以及代码下载地址:链接:http://pan.baidu.com/s/1c1OdNs0 密码:87k6
欢迎python爱好者加入:学习交流群 667279387
Pandas学习(二)——双色球开奖数据分析的更多相关文章
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- python 爬虫爬取历年双色球开奖信息
目前写的这些爬虫都是些静态网页,对于一些高级网页(像经过JS渲染过的页面),目前技术并不能解决,自己也是在慢慢学习过程中,如有错误,欢迎指正: 对面前端知识本人并不懂,过程中如果涉及到前端知识,也是百 ...
- numpy、pandas学习二
#numpy中arrary与pandas中series.DataFrame区别#arrary生成数组,无索引.列名:series有索引,且仅能创建一维数组:DataFrame有索引.列名import ...
- 用Python爬取双色球开奖信息,了解一下
1工具 2具体方法 1.使用python2.7编写爬取脚本 这里除了正常的爬取操作,还增加了独立的参数设定.如果没有参数,爬取的数据就在当前目录下:如果有参数,可以设定保存目录.保存文件名后缀 ...
- 第一个Python程序 | 机选彩票号码+爬取最新开奖号码
(机选彩票号码+爬取最新开奖号码 | 2021-04-21) 学习记录,好记不如烂笔头 这个程序作用是<机选三种彩票类型的号码> 程序内包含功能有如下: 自动获取最新的三种彩票的开奖号码 ...
- 中国福利彩票,牛B,开奖和数据传输有什么关系?
昨天,由中国教育电视台直播的福利彩票“双色球”15011期开奖,在没有事先预告的情况下突然取消.晚上11点40分左右,中国福利彩票发行管理中心唯一指定网络信息发布媒体——中彩网官方微博出乎意料地在网上 ...
- python爬虫学习之使用XPath解析开奖网站
实例需求:运用python语言爬取http://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html这个开奖网站所有的信息,并且保存为txt文件. 实例环境:pyth ...
- python爬虫学习之使用BeautifulSoup库爬取开奖网站信息-模块化
实例需求:运用python语言爬取http://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html这个开奖网站所有的信息,并且保存为txt文件和excel文件. 实 ...
- 【转】Pandas学习笔记(二)选择数据
Pandas学习笔记系列: Pandas学习笔记(一)基本介绍 Pandas学习笔记(二)选择数据 Pandas学习笔记(三)修改&添加值 Pandas学习笔记(四)处理丢失值 Pandas学 ...
随机推荐
- linux shell脚本语法笔记
1.&,&&,|,|| &:除了最后一个cmd,前面的cmd均已后台方式静默执行,执行结果显示在终端上,个别的cmd错误不影响整个命令的执行,全部的cmd同时执行 &a ...
- css3的过渡和动画的属性介绍
一.过渡 什么是过渡? 过渡是指:某元素的css属性值在一段时间内,平滑过渡到另外一个值,过渡主要观察的是过程和结果. 设置能够过渡的属性: 支持过渡的样式属性,颜色的属性,取值为数值,transfo ...
- 后台开发小白必学服务器框架——UDPServer
毕业后加入了一家大型的互联网公司的音视频产品部门做后台开发,其实我本身是学习自动化的,研究生的方向嵌入式系统,对互联网可是一知半解,因此能进入这样一个大公司还是很幸运的. 刚开始工作的半年应该是在上份 ...
- nyoj 28-大数阶乘 (大数模板)
28-大数阶乘 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:19 submit:39 题目描述: 我们都知道如何计算一个数的阶乘,可是,如果这个数 ...
- C语言|博客作业02
这个作业属于哪个课程 C程序语言设计 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-1/homework/8654 我在这个课程的目标是 ...
- Bootstrap3中的affix的使用Demo
<div class="container"> <div class="col-md-3"> <ul class="li ...
- AutoCad 二次开发 文字镜像
AutoCad 二次开发 文字镜像 参考:https://adndevblog.typepad.com/autocad/2013/10/mirroring-a-dbtext-entity.html 在 ...
- 机器学习 TensorFlow 实现智能鉴黄
前言 最近在做一款图床服务,关注公号的小伙伴一定记得小柒曾说过,会在周末放出的,不好意思放大家鸽子了.之所以一直没敢放出,是因为鉴黄接口一直没调试好,虽然我对公号的小伙伴百分之百信任,奈何互联网鱼龙混 ...
- UCACO刷题
UCACO刷题 SUBMIT: /* ID: your_id_here LANG: C++ TASK: test */ 文件:freopen(“file.in", "r" ...
- Java内存模型与volatile关键字
Java内存模型与volatile关键字 一).并发程序开发 并行程序的开发要涉及多线程.多任务间的协作和数据共享问题. 常用的并发控制:内部锁.重入锁.读写锁.信号量. 二).线程的特点 线程的特点 ...