1-pandas介绍
1:pandas 介绍
- 2008年WesMcKinney开发出的库
- 专门用于数据挖掘的开源python库
- 以Numpy为基础,借力Numpy模块在计算方面性能高的优势
- 基于matplotlib,能够简便的画图
- 独特的数据结构
2:为什么使用pandas
- 便捷的数据处理能力
- 读取文件方便
- 封装了Matplotlib、Numpy的画图和计算
3:案列
创建一个符合正太分布的10个股票5天的涨跌幅数据
import pandas as pd import numpy as np , # 创建一个符合正太分布的10个股票5天的涨跌幅数据 stock_change = np.random.normal(0,1,(10,5)) stock_change array([[-0.28043833, -1.32618229, -0.02362175, -0.20094603, 0.73294529], [ 0.3112984 , 1.02195314, 1.83917576, 0.11195336, -0.71334307], [-1.11658247, -0.36826343, -0.09732063, 1.02642364, -0.45189628], [-1.46312303, -0.38870085, -0.29937979, 1.39809135, -0.29268142], [ 1.46594285, -0.15487364, 0.54051497, 0.93455721, -0.78488885], [-0.97427856, -0.17405788, -0.19173299, -1.11578472, 1.21075393], [-1.15417931, 1.24306906, -0.2191712 , -1.49959002, 0.71535429], [ 0.91310418, 0.69221889, -2.05595283, -2.46365018, 0.1180998 ], [ 0.27667615, -0.91727713, 0.96849262, -1.80839189, 1.2208173 ], [-0.91819014, -0.52027355, -0.95796782, 0.01350831, -0.89826695]])
如何让数据更有意义的显示?处理刚才的股票数据
stock_change_rise = pd.DataFrame(stock_change) stock_change_rstock_change_riseise 0 1 2 3 4 0 -0.280438 -1.326182 -0.023622 -0.200946 0.732945 1 0.311298 1.021953 1.839176 0.111953 -0.713343 2 -1.116582 -0.368263 -0.097321 1.026424 -0.451896 3 -1.463123 -0.388701 -0.299380 1.398091 -0.292681 4 1.465943 -0.154874 0.540515 0.934557 -0.784889 5 -0.974279 -0.174058 -0.191733 -1.115785 1.210754 6 -1.154179 1.243069 -0.219171 -1.499590 0.715354 7 0.913104 0.692219 -2.055953 -2.463650 0.118100 8 0.276676 -0.917277 0.968493 -1.808392 1.220817 9 -0.918190 -0.520274 -0.957968 0.013508 -0.898267
要是给他添加行索引和列索引,就更为完美
添加行索引
# 构造行索引 #行数 lines = stock_change_rise.shape[0] print(lines)#10 stock_code = ["股票{}".format(i) for i in range(lines)] stock_code #['股票0', '股票1', '股票2', '股票3', '股票4', '股票5', '股票6', '股票7', '股票8', '股票9'] #添加行索引 data = pd.DataFrame(stock_change,index=stock_code) data 0 1 2 3 4 股票0 -0.280438 -1.326182 -0.023622 -0.200946 0.732945 股票1 0.311298 1.021953 1.839176 0.111953 -0.713343 股票2 -1.116582 -0.368263 -0.097321 1.026424 -0.451896 股票3 -1.463123 -0.388701 -0.299380 1.398091 -0.292681 股票4 1.465943 -0.154874 0.540515 0.934557 -0.784889 股票5 -0.974279 -0.174058 -0.191733 -1.115785 1.210754 股票6 -1.154179 1.243069 -0.219171 -1.499590 0.715354 股票7 0.913104 0.692219 -2.055953 -2.463650 0.118100 股票8 0.276676 -0.917277 0.968493 -1.808392 1.220817 股票9 -0.918190 -0.520274 -0.957968 0.013508 -0.898267
增加列索引
股票的日期是一个时间的序列,我们要实现从前往后的时间还要考虑每月的总天数等,不方便。使用pd.date_range():用于生成一组连续的时间序列(暂时了解)
date_range(start=None,end=None, periods=None, freq='B') start:开始时间 end:结束时间 periods:时间天数 freq:递进单位,默认1天,'B'默认略过周末
date = pd.date_range("2021-06-01",periods=data.shape[1],freq="B") date DatetimeIndex(['2021-06-01', '2021-06-02', '2021-06-03', '2021-06-04', '2021-06-07'], dtype='datetime64[ns]', freq='B') data = pd.DataFrame(stock_change,index=stock_code,columns=date) data 2021-06-01 2021-06-02 2021-06-03 2021-06-04 2021-06-07 股票0 -0.280438 -1.326182 -0.023622 -0.200946 0.732945 股票1 0.311298 1.021953 1.839176 0.111953 -0.713343 股票2 -1.116582 -0.368263 -0.097321 1.026424 -0.451896 股票3 -1.463123 -0.388701 -0.299380 1.398091 -0.292681 股票4 1.465943 -0.154874 0.540515 0.934557 -0.784889 股票5 -0.974279 -0.174058 -0.191733 -1.115785 1.210754 股票6 -1.154179 1.243069 -0.219171 -1.499590 0.715354 股票7 0.913104 0.692219 -2.055953 -2.463650 0.118100 股票8 0.276676 -0.917277 0.968493 -1.808392 1.220817 股票9 -0.918190 -0.520274 -0.957968 0.013508 -0.898267
4 DataFrame
4.1 DataFrame结构
DataFrame对象既有行索引,又有列索引
- 行索引,表明不同行,横向索引,叫index,0轴,axis=0
- 列索引,表名不同列,纵向索引,叫columns,1轴,axis=1
4.2 DatatFrame的属性
- shape
data.shape # 结果 (10, 5)
- index
DataFrame的行索引列表 data.index Index(['股票0', '股票1', '股票2', '股票3', '股票4', '股票5', '股票6', '股票7', '股票8', '股票9'], dtype='object')
- columns
DataFrame的列索引列表 data.columns DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04', '2017-01-05', '2017-01-06'], dtype='datetime64[ns]', freq='B')
- values
直接获取其中array的值 data.values array([[-0.06544031, -1.30931491, -1.45451514, 0.57973008, 1.48602405], [-1.73216741, -0.83413717, 0.45861517, -0.80391793, -0.46878575], [ 0.21805567, 0.19901371, 0.7134683 , 0.5484263 , 0.38623412], [-0.42207879, -0.33702398, 0.42328531, -1.23079202, 1.32843773], [-1.72530711, 0.07591832, -1.91708358, -0.16535818, 1.07645091], [-0.81576845, -0.28675278, 1.20441981, 0.73365951, -0.06214496], [-0.98820861, -1.01815231, -0.95417342, -0.81538991, 0.50268175], [-0.10034128, 0.61196204, -0.06850331, 0.74738433, 0.143011 ], [ 1.00026175, 0.34241958, -2.2529711 , 0.93921064, 1.14080312], [ 2.52064693, 1.55384756, 1.72252984, 0.61270132, 0.60888092]])
- T
转置
data.T
结果
股票0 股票1 股票2 股票3 股票4 股票5 股票6 股票7 股票8 股票9
2021-06-01 -0.280438 0.311298 -1.116582 -1.463123 1.465943 -0.974279 -1.154179 0.913104 0.276676 -0.918190
2021-06-02 -1.326182 1.021953 -0.368263 -0.388701 -0.154874 -0.174058 1.243069 0.692219 -0.917277 -0.520274
2021-06-03 -0.023622 1.839176 -0.097321 -0.299380 0.540515 -0.191733 -0.219171 -2.055953 0.968493 -0.957968
2021-06-04 -0.200946 0.111953 1.026424 1.398091 0.934557 -1.115785 -1.499590 -2.463650 -1.808392 0.013508
2021-06-07 0.732945 -0.713343 -0.451896 -0.292681 -0.784889 1.210754 0.715354 0.118100 1.220817 -0.898267
- head(5):显示前5行内容
如果不补充参数,默认5行。填入参数N则显示前N行 data.head(5) 2017-01-02 00:00:00 2017-01-03 00:00:00 2017-01-04 00:00:00 2017-01-05 00:00:00 2017-01-06 00:00:00 股票0 -0.065440 -1.309315 -1.454515 0.579730 1.486024 股票1 -1.732167 -0.834137 0.458615 -0.803918 -0.468786 股票2 0.218056 0.199014 0.713468 0.548426 0.386234 股票3 -0.422079 -0.337024 0.423285 -1.230792 1.328438 股票4 -1.725307 0.075918 -1.917084 -0.165358 1.076451
- tail(5):显示后5行内容
如果不补充参数,默认5行。填入参数N则显示后N行 data.tail(5) 2017-01-02 00:00:00 2017-01-03 00:00:00 2017-01-04 00:00:00 2017-01-05 00:00:00 2017-01-06 00:00:00 股票5 -0.815768 -0.286753 1.204420 0.733660 -0.062145 股票6 -0.988209 -1.018152 -0.954173 -0.815390 0.502682 股票7 -0.100341 0.611962 -0.068503 0.747384 0.143011 股票8 1.000262 0.342420 -2.252971 0.939211 1.140803 股票9 2.520647 1.553848 1.722530 0.612701 0.608881
4.3 DatatFrame索引的设置
4.3.1修改行列索引值
stock_code = ["股票_" + str(i) for i in range(stock_day_rise.shape[0])] # 必须整体全部修改 data.index = stock_code 结果 2017-01-02 00:00:00 2017-01-03 00:00:00 2017-01-04 00:00:00 2017-01-05 00:00:00 2017-01-06 00:00:00 股票_0 -0.065440 -1.309315 -1.454515 0.579730 1.486024 股票_1 -1.732167 -0.834137 0.458615 -0.803918 -0.468786 股票_2 0.218056 0.199014 0.713468 0.548426 0.386234 股票_3 -0.422079 -0.337024 0.423285 -1.230792 1.328438 股票_4 -1.725307 0.075918 -1.917084 -0.165358 1.076451 股票_5 -0.815768 -0.286753 1.204420 0.733660 -0.062145 股票_6 -0.988209 -1.018152 -0.954173 -0.815390 0.502682 股票_7 -0.100341 0.611962 -0.068503 0.747384 0.143011 股票_8 1.000262 0.342420 -2.252971 0.939211 1.140803 股票_9 2.520647 1.553848 1.722530 0.612701 0.608881 注意:以下修改方式是错误的 # 错误修改方式 data.index[3] = '股票_3'
4.3.2 重设索引
- reset_index(drop=False)
- 设置新的下标索引
- drop:默认为False,不删除原来索引,如果为True,删除原来的索引值
# 重置索引,drop=False data.reset_index() index 2017-01-02 00:00:00 2017-01-03 00:00:00 2017-01-04 00:00:00 2017-01-05 00:00:00 2017-01-06 00:00:00 0 股票_0 -0.065440 -1.309315 -1.454515 0.579730 1.486024 1 股票_1 -1.732167 -0.834137 0.458615 -0.803918 -0.468786 2 股票_2 0.218056 0.199014 0.713468 0.548426 0.386234 3 股票_3 -0.422079 -0.337024 0.423285 -1.230792 1.328438 4 股票_4 -1.725307 0.075918 -1.917084 -0.165358 1.076451 5 股票_5 -0.815768 -0.286753 1.204420 0.733660 -0.062145 6 股票_6 -0.988209 -1.018152 -0.954173 -0.815390 0.502682 7 股票_7 -0.100341 0.611962 -0.068503 0.747384 0.143011 8 股票_8 1.000262 0.342420 -2.252971 0.939211 1.140803 9 股票_9 2.520647 1.553848 1.722530 0.612701 0.608881 # 重置索引,drop=True data.reset_index(drop=True) 2017-01-02 00:00:00 2017-01-03 00:00:00 2017-01-04 00:00:00 2017-01-05 00:00:00 2017-01-06 00:00:00 0 -0.065440 -1.309315 -1.454515 0.579730 1.486024 1 -1.732167 -0.834137 0.458615 -0.803918 -0.468786 2 0.218056 0.199014 0.713468 0.548426 0.386234 3 -0.422079 -0.337024 0.423285 -1.230792 1.328438 4 -1.725307 0.075918 -1.917084 -0.165358 1.076451 5 -0.815768 -0.286753 1.204420 0.733660 -0.062145 6 -0.988209 -1.018152 -0.954173 -0.815390 0.502682 7 -0.100341 0.611962 -0.068503 0.747384 0.143011 8 1.000262 0.342420 -2.252971 0.939211 1.140803 9 2.520647 1.553848 1.722530 0.612701 0.608881
4.3.3 以某列值设置为新的索引
-
set_index(keys, drop=True)
- keys : 列索引名成或者列索引名称的列表
- drop : boolean, default True.当做新的索引,删除原来的列
-
设置新索引案例
1、创建 df = pd.DataFrame({'month': [1, 4, 7, 10], 'year': [2012, 2014, 2013, 2014], 'sale':[55, 40, 84, 31]}) month sale year 0 1 55 2012 1 4 40 2014 2 7 84 2013 3 10 31 2014 2、以月份设置新的索引 df.set_index('month') sale year month 1 55 2012 4 40 2014 7 84 2013 10 31 2014 3、设置多个索引,以年和月份 df.set_index(['year', 'month']) sale year month 2012 1 55 2014 4 40 2013 7 84 2014 10 31
注:通过刚才的设置,这样DataFrame就变成了一个具有MultiIndex的DataFrame。
5 MultiIndex与Panel
打印刚才的df的行索引结果
df.index MultiIndex(levels=[[1, 2], [1, 4, 7, 10]], labels=[[0, 0, 1, 1], [0, 1, 2, 3]], names=['year', 'month'])
5.1 MultiIndex
多级或分层索引对象。
- index属性
- names:levels的名称
- levels:每个level的元组值
df.index.names FrozenList(['year', 'month']) df.index.levels FrozenList([[1, 2], [1, 4, 7, 10]])
5.2 Panel
- class
pandas.Panel
(data=None, items=None, major_axis=None, minor_axis=None, copy=False, dtype=None)- 存储3维数组的Panel结构
p = pd.Panel(np.arange(24).reshape(4,3,2), items=list('ABCD'), major_axis=pd.date_range('20130101', periods=3), minor_axis=['first', 'second']) p <class 'pandas.core.panel.Panel'> Dimensions: 4 (items) x 3 (major_axis) x 2 (minor_axis) Items axis: A to D Major_axis axis: 2013-01-01 00:00:00 to 2013-01-03 00:00:00 Minor_axis axis: first to second
- items -
axis 0
,每个项目对应于内部包含的数据帧(DataFrame)。 - major_axis -
axis 1
,它是每个数据帧(DataFrame)的索引(行)。 - minor_axis -
axis 2
,它是每个数据帧(DataFrame)的列。
查看panel数据:
p[:,:,"first"] p["B",:,:]
注:Pandas从版本0.20.0开始弃用:推荐的用于表示3D数据的方法是通过DataFrame上的MultiIndex方法
6 Series结构
什么是Series结构呢,我们直接看下面的图:
- series结构只有行索引
我们将之前的涨跌幅数据进行转置,然后获取'股票0'的所有数据
# series
type(data['2017-01-02'])
pandas.core.series.Series
# 这一步相当于是series去获取行索引的值
data['2017-01-02']['股票_0']
-0.18753158283513574
6.1 创建series
通过已有数据创建
- 指定内容,默认索引
pd.Series(np.arange(10))
- 指定索引
pd.Series([6.7,5.6,3,10,2], index=[1,2,3,4,5])
通过字典数据创建
pd.Series({'red':100, ''blue':200, 'green': 500, 'yellow':1000})
6.2 series获取属性和值
- index
- values