Pandas的分层索引MultiIndex怎样用?

Pandas的分层索引MultiIndex

为什么要学习分层索引MultiIndex?
* 分层索引:在一个轴向上拥有多个索引层级,可以表达更高维度数据的形式;
* 可以更方便的进行数据筛选,如果有序则性能更好;
* groupby等操作的结果,如果是多KEY,结果是分层索引,需要会使用
* 一般不需要自己创建分层索引(MultiIndex有构造函数但一般不用)

演示数据:百度、阿里巴巴、爱奇艺、京东四家公司的10天股票数据
数据来自:英为财经
https://cn.investing.com/

本次演示提纲:
一、Series的分层索引MultiIndex
二、Series有多层索引怎样筛选数据?
三、DataFrame的多层索引MultiIndex
四、DataFrame有多层索引怎样筛选数据?

import pandas as pd
%matplotlib inline
stocks = pd.read_excel('./datas/stocks/互联网公司股票.xlsx')
stocks.shape
(12, 8)
stocks.head(3)
日期公司收盘开盘交易量涨跌幅
02019-10-03BIDU104.32102.35104.73101.152.240.02
12019-10-02BIDU102.62100.85103.2499.502.690.01
22019-10-01BIDU102.00102.80103.26101.001.78-0.01
stocks["公司"].unique()
array(['BIDU', 'BABA', 'IQ', 'JD'], dtype=object)
stocks.index
RangeIndex(start=0, stop=12, step=1)
stocks.groupby('公司')["收盘"].mean()
公司
BABA    166.80
BIDU    102.98
IQ       15.90
JD       28.35
Name: 收盘, dtype: float64

一、Series的分层索引MultiIndex

ser = stocks.groupby(['公司', '日期'])['收盘'].mean()
ser
公司    日期        
BABA  2019-10-01    165.15
      2019-10-02    165.77
      2019-10-03    169.48
BIDU  2019-10-01    102.00
      2019-10-02    102.62
      2019-10-03    104.32
IQ    2019-10-01     15.92
      2019-10-02     15.72
      2019-10-03     16.06
JD    2019-10-01     28.19
      2019-10-02     28.06
      2019-10-03     28.80
Name: 收盘, dtype: float64

多维索引中,空白的意思是:使用上面的值

ser.index
MultiIndex([('BABA', '2019-10-01'),
            ('BABA', '2019-10-02'),
            ('BABA', '2019-10-03'),
            ('BIDU', '2019-10-01'),
            ('BIDU', '2019-10-02'),
            ('BIDU', '2019-10-03'),
            (  'IQ', '2019-10-01'),
            (  'IQ', '2019-10-02'),
            (  'IQ', '2019-10-03'),
            (  'JD', '2019-10-01'),
            (  'JD', '2019-10-02'),
            (  'JD', '2019-10-03')],
           names=['公司', '日期'])
# unstack把二级索引变成列
ser.unstack()
日期2019-10-012019-10-022019-10-03
公司
BABA165.15165.77169.48
BIDU102.00102.62104.32
IQ15.9215.7216.06
JD28.1928.0628.80
ser
公司    日期        
BABA  2019-10-01    165.15
      2019-10-02    165.77
      2019-10-03    169.48
BIDU  2019-10-01    102.00
      2019-10-02    102.62
      2019-10-03    104.32
IQ    2019-10-01     15.92
      2019-10-02     15.72
      2019-10-03     16.06
JD    2019-10-01     28.19
      2019-10-02     28.06
      2019-10-03     28.80
Name: 收盘, dtype: float64
ser.reset_index()
公司日期收盘
0BABA2019-10-01165.15
1BABA2019-10-02165.77
2BABA2019-10-03169.48
3BIDU2019-10-01102.00
4BIDU2019-10-02102.62
5BIDU2019-10-03104.32
6IQ2019-10-0115.92
7IQ2019-10-0215.72
8IQ2019-10-0316.06
9JD2019-10-0128.19
10JD2019-10-0228.06
11JD2019-10-0328.80

二、Series有多层索引MultiIndex怎样筛选数据?

ser
公司    日期        
BABA  2019-10-01    165.15
      2019-10-02    165.77
      2019-10-03    169.48
BIDU  2019-10-01    102.00
      2019-10-02    102.62
      2019-10-03    104.32
IQ    2019-10-01     15.92
      2019-10-02     15.72
      2019-10-03     16.06
JD    2019-10-01     28.19
      2019-10-02     28.06
      2019-10-03     28.80
Name: 收盘, dtype: float64
ser.loc['BIDU']
日期
2019-10-01    102.00
2019-10-02    102.62
2019-10-03    104.32
Name: 收盘, dtype: float64
# 多层索引,可以用元组的形式筛选
ser.loc[('BIDU', '2019-10-02')]
102.62
ser.loc[:, '2019-10-02']
公司
BABA    165.77
BIDU    102.62
IQ       15.72
JD       28.06
Name: 收盘, dtype: float64

三、DataFrame的多层索引MultiIndex

stocks.head()
日期公司收盘开盘交易量涨跌幅
02019-10-03BIDU104.32102.35104.73101.152.240.02
12019-10-02BIDU102.62100.85103.2499.502.690.01
22019-10-01BIDU102.00102.80103.26101.001.78-0.01
32019-10-03BABA169.48166.65170.18165.0010.390.02
42019-10-02BABA165.77162.82166.88161.9011.600.00
stocks.set_index(['公司', '日期'], inplace=True)
stocks
收盘开盘交易量涨跌幅
公司日期
BIDU2019-10-03104.32102.35104.73101.152.240.02
2019-10-02102.62100.85103.2499.502.690.01
2019-10-01102.00102.80103.26101.001.78-0.01
BABA2019-10-03169.48166.65170.18165.0010.390.02
2019-10-02165.77162.82166.88161.9011.600.00
2019-10-01165.15168.01168.23163.6414.19-0.01
IQ2019-10-0316.0615.7116.3815.3210.080.02
2019-10-0215.7215.8515.8715.128.10-0.01
2019-10-0115.9216.1416.2215.5011.65-0.01
JD2019-10-0328.8028.1128.9727.828.770.03
2019-10-0228.0628.0028.2227.539.530.00
2019-10-0128.1928.2228.5727.9710.640.00
stocks.index
MultiIndex([('BIDU', '2019-10-03'),
            ('BIDU', '2019-10-02'),
            ('BIDU', '2019-10-01'),
            ('BABA', '2019-10-03'),
            ('BABA', '2019-10-02'),
            ('BABA', '2019-10-01'),
            (  'IQ', '2019-10-03'),
            (  'IQ', '2019-10-02'),
            (  'IQ', '2019-10-01'),
            (  'JD', '2019-10-03'),
            (  'JD', '2019-10-02'),
            (  'JD', '2019-10-01')],
           names=['公司', '日期'])
stocks.sort_index(inplace=True)
stocks
收盘开盘交易量涨跌幅
公司日期
BABA2019-10-01165.15168.01168.23163.6414.19-0.01
2019-10-02165.77162.82166.88161.9011.600.00
2019-10-03169.48166.65170.18165.0010.390.02
BIDU2019-10-01102.00102.80103.26101.001.78-0.01
2019-10-02102.62100.85103.2499.502.690.01
2019-10-03104.32102.35104.73101.152.240.02
IQ2019-10-0115.9216.1416.2215.5011.65-0.01
2019-10-0215.7215.8515.8715.128.10-0.01
2019-10-0316.0615.7116.3815.3210.080.02
JD2019-10-0128.1928.2228.5727.9710.640.00
2019-10-0228.0628.0028.2227.539.530.00
2019-10-0328.8028.1128.9727.828.770.03

四、DataFrame有多层索引MultiIndex怎样筛选数据?

重要知识】在选择数据时:
* 元组(key1,key2)代表筛选多层索引,其中key1是索引第一级,key2是第二级,比如key1=JD, key2=2019-10-02
* 列表[key1,key2]代表同一层的多个KEY,其中key1和key2是并列的同级索引,比如key1=JD, key2=BIDU

stocks.loc['BIDU']
收盘开盘交易量涨跌幅
日期
2019-10-01102.00102.80103.26101.001.78-0.01
2019-10-02102.62100.85103.2499.502.690.01
2019-10-03104.32102.35104.73101.152.240.02
stocks.loc[('BIDU', '2019-10-02'), :]
收盘     102.62
开盘     100.85
高      103.24
低       99.50
交易量      2.69
涨跌幅      0.01
Name: (BIDU, 2019-10-02), dtype: float64
stocks.loc[('BIDU', '2019-10-02'), '开盘']
100.85
stocks.loc[['BIDU', 'JD'], :]
收盘开盘交易量涨跌幅
公司日期
BIDU2019-10-01102.00102.80103.26101.001.78-0.01
2019-10-02102.62100.85103.2499.502.690.01
2019-10-03104.32102.35104.73101.152.240.02
JD2019-10-0128.1928.2228.5727.9710.640.00
2019-10-0228.0628.0028.2227.539.530.00
2019-10-0328.8028.1128.9727.828.770.03
stocks.loc[(['BIDU', 'JD'], '2019-10-03'), :]
收盘开盘交易量涨跌幅
公司日期
BIDU2019-10-03104.32102.35104.73101.152.240.02
JD2019-10-0328.8028.1128.9727.828.770.03
stocks.loc[(['BIDU', 'JD'], '2019-10-03'), '收盘']
公司    日期        
BIDU  2019-10-03    104.32
JD    2019-10-03     28.80
Name: 收盘, dtype: float64
stocks.loc[('BIDU', ['2019-10-02', '2019-10-03']), '收盘']
公司    日期        
BIDU  2019-10-02    102.62
      2019-10-03    104.32
Name: 收盘, dtype: float64
# slice(None)代表筛选这一索引的所有内容
stocks.loc[(slice(None), ['2019-10-02', '2019-10-03']), :]
收盘开盘交易量涨跌幅
公司日期
BABA2019-10-02165.77162.82166.88161.9011.600.00
2019-10-03169.48166.65170.18165.0010.390.02
BIDU2019-10-02102.62100.85103.2499.502.690.01
2019-10-03104.32102.35104.73101.152.240.02
IQ2019-10-0215.7215.8515.8715.128.10-0.01
2019-10-0316.0615.7116.3815.3210.080.02
JD2019-10-0228.0628.0028.2227.539.530.00
2019-10-0328.8028.1128.9727.828.770.03
stocks.reset_index()
公司日期收盘开盘交易量涨跌幅
0BABA2019-10-01165.15168.01168.23163.6414.19-0.01
1BABA2019-10-02165.77162.82166.88161.9011.600.00
2BABA2019-10-03169.48166.65170.18165.0010.390.02
3BIDU2019-10-01102.00102.80103.26101.001.78-0.01
4BIDU2019-10-02102.62100.85103.2499.502.690.01
5BIDU2019-10-03104.32102.35104.73101.152.240.02
6IQ2019-10-0115.9216.1416.2215.5011.65-0.01
7IQ2019-10-0215.7215.8515.8715.128.10-0.01
8IQ2019-10-0316.0615.7116.3815.3210.080.02
9JD2019-10-0128.1928.2228.5727.9710.640.00
10JD2019-10-0228.0628.0028.2227.539.530.00
11JD2019-10-0328.8028.1128.9727.828.770.03

转载请注明链接: http://www.crazyant.net/2598.html

相关推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注