pandas学习(二)——dataframe数据的选取

本章主要利用双色球开奖数据来学习pandas的DataFrame数据选取,Series的统计功能,以及matplotlib画柱状图。

# -*- coding: utf-8 -*-
import pandas as pd
import numpy  as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# 读取数据
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)

# 输入期数,获取历史数据,本例获取历年的079期数据
history_data = all_data[[x.endswith('079') for x in all_data['index']]].copy()
history_red_ball = history_data.iloc[:, 1:6]
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()

获取出来的历史数据如下所示:

        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、红球历史数据次数统计图

20170827211827424.png

2、蓝球历史数据次数统计图

20170827211843209.png

数据以及代码下载地址:链接:http://pan.baidu.com/s/1c1OdNs0 密码:87k6

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容