Pandas 索引
pandas 的索引 index 的用途
使用index 查询数据
python
# drop == False, 让索引列还保持在column
df.set_index("xxx", inplace=True, drop=False) #drop=False:表示保留索引列,并将其作为新的DataFrame的一个普通列。
# 使用index 查询
df.loc[xxx].head(x)
# 注意: 使用index 会提升查询性能
使用index 查询 需要优先对 index 进行排序 否则查询复杂度会很高,耗时长
# 将数据随机打散
from sklearn.utils import shuffle #shuffle函数用于将数据集随机打乱。
df_shuffle = shuffle(df)
# 将index 进行排序
df_sort = df_shuffle.sort_index() # 默认为升序
df_sort = df_shuffle.sort_index(ascending=False) #索引改为降序
# 检查索引是否为递增
df_shuffle.index.is_monotonic_increasing
# 检查索引是否唯一 如果不是唯一 则会使用二分法查询
df_shuffle.index.is_unique
# 计时,查询id=500 的数据查询性能
%timeit df_shuffle.loc[500]
总结:
%timeit 是一个Python魔法命令,用于计算代码执行所需的时间。
使用index 自动对齐数据
python
import pandas as pd
s1 = pd.Series([1,2,3], index=list("abc"))
s2 = pd.Series([2,3,4], index=list("bcd"))
s1 +s2
a NaN
b 4.0
c 6.0
d NaN
dtype: float64
pandas的分层索引
pandas 的分层索引 Multilndex
- 分层索引:在一个轴向上拥有多个索引层级,可以表达更高维度的数据形式
- 可以更方便的进行数据筛选,如果有序,则性能更好
- groupby等操作的结果,如果是多key,结果是分层索引,需要会使用
- 一般不需要自己创建分层索引(multilndex有构造函数,但一般不用)
python
import pandas as pd
%matplotlib inline
stock_history = pd.read_csv('./quotefile/股票历史数据.csv')
日期 | 公司 | 收盘 | 开盘 | 高 | 低 | 交易量 | 涨跌幅 | |
---|---|---|---|---|---|---|---|---|
0 | 2024/3/22 | IQY | 4.04 | 4.04 | 4.14 | 3.96 | 7.51 | -0.0074 |
1 | 2024/3/21 | IQY | 4.07 | 4.11 | 4.22 | 4.07 | 5.00 | -0.0216 |
2 | 2024/3/20 | IQY | 4.16 | 4.00 | 4.16 | 3.99 | 6.69 | 0.0532 |
3 | 2024/3/19 | IQY | 3.95 | 3.90 | 3.99 | 3.85 | 5.24 | -0.0075 |
4 | 2024/3/18 | IQY | 3.98 | 4.09 | 4.09 | 3.94 | 5.44 | -0.0050 |
... | ... | ... | ... | ... | ... | ... | ... | ... |
75 | 2024/3/1 | JD | 23.00 | 23.00 | 23.27 | 22.85 | 13.00 | 0.0168 |
76 | 2024/2/29 | JD | 22.62 | 22.85 | 23.11 | 22.52 | 14.57 | -0.0083 |
77 | 2024/2/28 | JD | 22.81 | 23.33 | 23.40 | 22.77 | 15.19 | -0.0527 |
78 | 2024/2/27 | JD | 24.08 | 24.07 | 24.35 | 23.93 | 9.34 | 0.0160 |
79 | 2024/2/26 | JD | 23.70 | 23.77 | 24.02 | 23.59 | 6.75 | -0.0084 |
80 rows × 8 columns
Series的分层索引 Multilndex
python
ser = stock_history.groupby(['公司', '日期'])['收盘'].mean()
ser
# unstack 把二级索引变成列
ser.unstack()
日期 | 2024/2/26 | 2024/2/27 | 2024/2/28 | 2024/2/29 | 2024/3/1 | 2024/3/11 | 2024/3/12 | 2024/3/13 | 2024/3/14 | 2024/3/15 | 2024/3/18 | 2024/3/19 | 2024/3/20 | 2024/3/21 | 2024/3/22 | 2024/3/4 | 2024/3/5 | 2024/3/6 | 2024/3/7 | 2024/3/8 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
公司 | ||||||||||||||||||||
BABA | 76.51 | 77.68 | 74.59 | 74.03 | 74.62 | 74.86 | 76.06 | 76.39 | 73.40 | 73.42 | 73.52 | 73.42 | 74.18 | 73.83 | 72.13 | 72.00 | 72.07 | 73.71 | 72.54 | 73.55 |
BAIDU | 110.59 | 112.36 | 103.31 | 101.33 | 104.02 | 101.29 | 105.27 | 107.89 | 104.64 | 103.86 | 104.77 | 104.17 | 103.89 | 101.70 | 102.18 | 100.72 | 98.25 | 99.38 | 97.81 | 98.27 |
IQY | 3.51 | 3.67 | 3.67 | 3.69 | 3.82 | 4.07 | 4.30 | 4.16 | 4.00 | 4.00 | 3.98 | 3.95 | 4.16 | 4.07 | 4.04 | 3.64 | 3.45 | 3.73 | 3.77 | 3.94 |
JD | 23.70 | 24.08 | 22.81 | 22.62 | 23.00 | 26.07 | 27.37 | 28.01 | 27.05 | 27.45 | 27.85 | 27.59 | 28.14 | 27.05 | 26.50 | 21.88 | 21.44 | 24.91 | 23.99 | 24.75 |
Series 有多层索引 multilndex 怎样筛选数据
python
ser.loc['BAIDU']
ser.loc[:, '2024/2/26']
# 多层索引,可以用元组的形式筛选
ser.loc[('BAIDU', '2024/2/26')]
DF 的多层索引 multilndex 以及如何筛选数据
注意: 在选择数据时
列表'[key1, key2]' 代表同一层的多个key
元组(key1, key2)代表多层索引
python
stock_history.set_index(['公司', '日期'], inplace=True)
stock_history
收盘 | 开盘 | 高 | 低 | 交易量 | 涨跌幅 | ||
---|---|---|---|---|---|---|---|
公司 | 日期 | ||||||
IQY | 2024/3/22 | 4.04 | 4.04 | 4.14 | 3.96 | 7.51 | -0.0074 |
2024/3/21 | 4.07 | 4.11 | 4.22 | 4.07 | 5.00 | -0.0216 | |
2024/3/20 | 4.16 | 4.00 | 4.16 | 3.99 | 6.69 | 0.0532 | |
2024/3/19 | 3.95 | 3.90 | 3.99 | 3.85 | 5.24 | -0.0075 | |
2024/3/18 | 3.98 | 4.09 | 4.09 | 3.94 | 5.44 | -0.0050 | |
... | ... | ... | ... | ... | ... | ... | ... |
JD | 2024/3/1 | 23.00 | 23.00 | 23.27 | 22.85 | 13.00 | 0.0168 |
2024/2/29 | 22.62 | 22.85 | 23.11 | 22.52 | 14.57 | -0.0083 | |
2024/2/28 | 22.81 | 23.33 | 23.40 | 22.77 | 15.19 | -0.0527 | |
2024/2/27 | 24.08 | 24.07 | 24.35 | 23.93 | 9.34 | 0.0160 | |
2024/2/26 | 23.70 | 23.77 | 24.02 | 23.59 | 6.75 | -0.0084 |
80 rows × 6 columns
python
stock_history.sort_index(inplace=True)
stock_history
收盘 | 开盘 | 高 | 低 | 交易量 | 涨跌幅 | ||
---|---|---|---|---|---|---|---|
公司 | 日期 | ||||||
BABA | 2024/2/26 | 76.51 | 75.62 | 76.57 | 75.34 | 13.90 | 0.0072 |
2024/2/27 | 77.68 | 77.53 | 78.05 | 77.25 | 14.39 | 0.0153 | |
2024/2/28 | 74.59 | 76.02 | 76.18 | 74.45 | 16.80 | -0.0398 | |
2024/2/29 | 74.03 | 74.68 | 75.04 | 73.80 | 12.44 | -0.0075 | |
2024/3/1 | 74.62 | 74.48 | 75.09 | 74.37 | 11.98 | 0.0080 | |
... | ... | ... | ... | ... | ... | ... | ... |
JD | 2024/3/4 | 21.88 | 22.79 | 22.83 | 21.60 | 28.39 | -0.0487 |
2024/3/5 | 21.44 | 21.34 | 21.67 | 21.18 | 18.28 | -0.0201 | |
2024/3/6 | 24.91 | 24.85 | 25.67 | 24.33 | 54.34 | 0.1618 | |
2024/3/7 | 23.99 | 24.52 | 24.54 | 23.65 | 17.45 | -0.0369 | |
2024/3/8 | 24.75 | 23.87 | 24.95 | 23.78 | 26.01 | 0.0317 |
80 rows × 6 columns
python
stock_history.loc['BAIDU'] # 筛选第一层索引
stock_history.loc[(['BAIDU','JD'], '2024/2/27'), :] # 第一层 百度和京东 第二层 日期 返回所有值
# slice(None) 代表筛选索引的所有内容
stock_history.loc[(slice(None), ['2024/2/28','2024/2/29']),:]
收盘 | 开盘 | 高 | 低 | 交易量 | 涨跌幅 | ||
---|---|---|---|---|---|---|---|
公司 | 日期 | ||||||
BABA | 2024/2/28 | 74.59 | 76.02 | 76.18 | 74.45 | 16.80 | -0.0398 |
2024/2/29 | 74.03 | 74.68 | 75.04 | 73.80 | 12.44 | -0.0075 | |
BAIDU | 2024/2/28 | 103.31 | 107.85 | 108.09 | 103.25 | 11.32 | -0.0805 |
2024/2/29 | 101.33 | 103.07 | 104.00 | 100.85 | 7.12 | -0.0192 | |
IQY | 2024/2/28 | 3.67 | 3.89 | 3.95 | 3.57 | 21.16 | 0.0000 |
2024/2/29 | 3.69 | 3.70 | 3.90 | 3.62 | 17.09 | 0.0054 | |
JD | 2024/2/28 | 22.81 | 23.33 | 23.40 | 22.77 | 15.19 | -0.0527 |
2024/2/29 | 22.62 | 22.85 | 23.11 | 22.52 | 14.57 | -0.0083 |