pandas回顾 1 文件的读取 索引与切片
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
读取包含多层级索引的excel,使用header参数,header = [index1,index2…] 多层级使用,index1…index2表示多级索引的行, header = None 无列标签时使用
pd.read_excel('业务表.xlsx',header=[0,1])
城市 |
上半年 |
|
|
下半年 |
|
|
|
90# |
93# |
97# |
90# |
93# |
97# |
成都 |
8000 |
9000 |
3000 |
9000 |
10000 |
3200 |
绵阳 |
5600 |
7000 |
1600 |
5800 |
8000 |
2100 |
汶川 |
4000 |
5000 |
2000 |
4600 |
7800 |
1700 |
攀枝花 |
3200 |
4500 |
1000 |
3400 |
3800 |
2000 |
sheet_name 用于指定表格当中sheet的编号,0表示sheet1,1表示sheet2。。。,index_col 表示以哪一列作为行索引
sheet2 = pd.read_excel('业务表.xlsx',sheet_name=1,header=None,index_col=0)
sheet2
0 |
1 |
2 |
3 |
4 |
5 |
6 |
成都 |
8000 |
9000 |
3000 |
9000 |
10000 |
3200 |
绵阳 |
5600 |
7000 |
1600 |
5800 |
8000 |
2100 |
汶川 |
4000 |
5000 |
2000 |
4600 |
7800 |
1700 |
攀枝花 |
3200 |
4500 |
1000 |
3400 |
3800 |
2000 |
# 设置某一列为行索引,结果与上面index_col结果一致
# sheet2.set_index(0)
# 把行索引设置为新的列
sheet2.reset_index()
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
0 |
成都 |
8000 |
9000 |
3000 |
9000 |
10000 |
3200 |
1 |
绵阳 |
5600 |
7000 |
1600 |
5800 |
8000 |
2100 |
2 |
汶川 |
4000 |
5000 |
2000 |
4600 |
7800 |
1700 |
3 |
攀枝花 |
3200 |
4500 |
1000 |
3400 |
3800 |
2000 |
column = pd.MultiIndex.from_product([['上半年','下半年'],['90#','93#','97#']])
# 使用dataFrame的columns属性,来对表格索引重新赋值
sheet2.columns = column
sheet2
|
上半年 |
|
|
下半年 |
|
|
0 |
90# |
93# |
97# |
90# |
93# |
97# |
成都 |
8000 |
9000 |
3000 |
9000 |
10000 |
3200 |
绵阳 |
5600 |
7000 |
1600 |
5800 |
8000 |
2100 |
汶川 |
4000 |
5000 |
2000 |
4600 |
7800 |
1700 |
攀枝花 |
3200 |
4500 |
1000 |
3400 |
3800 |
2000 |
多级索引的赋值使用如下方法
sheet2.loc['成都',('上半年','90#')] = 8900
# 这种属于跨级访问,相当于对sheet2['上半年']产生的临时对象赋值 不推荐使用
sheet2['上半年'].loc['成都','90#'] = 18000
多级索引赋值还可以采用如下办法
sheet2_copy = sheet2['上半年'].copy()
sheet2_copy.loc['成都','90#'] = 9700
sheet2['上半年'] = sheet2_copy
sheet2
|
上半年 |
|
|
下半年 |
|
|
0 |
90# |
93# |
97# |
90# |
93# |
97# |
成都 |
9700 |
9000 |
3000 |
9000 |
10000 |
3200 |
绵阳 |
5600 |
7000 |
1600 |
5800 |
8000 |
2100 |
汶川 |
4000 |
5000 |
2000 |
4600 |
7800 |
1700 |
攀枝花 |
3200 |
4500 |
1000 |
3400 |
3800 |
2000 |
sheet2_copy
0 |
90# |
93# |
97# |
成都 |
9700 |
9000 |
3000 |
绵阳 |
5600 |
7000 |
1600 |
汶川 |
4000 |
5000 |
2000 |
攀枝花 |
3200 |
4500 |
1000 |
显式访问
- 访问元素
sheet2_copy.loc['绵阳']['97#']
sheet2_copy['97#'].loc['绵阳']
sheet2_copy.loc['绵阳','97#']
前面两种只限于访问,不能复制,都属于跨级访问
- 访问行
sheet2_copy.loc['行索引']
sheet2_copy.loc[index1:index2]
- 访问列
sheet2_copy['列索引']
sheet2_copy.loc[:,column1:column2]
# dataframe的列切片要从行的方向着手
sheet2_copy.loc[:,'93#':'97#']
0 |
93# |
97# |
成都 |
9000 |
3000 |
绵阳 |
7000 |
1600 |
汶川 |
5000 |
2000 |
攀枝花 |
4500 |
1000 |
sheet2_copy.loc['成都':'汶川','90#':'97#']
0 |
90# |
93# |
97# |
成都 |
9700 |
9000 |
3000 |
绵阳 |
5600 |
7000 |
1600 |
汶川 |
4000 |
5000 |
2000 |
隐式访问
1.访问元素
sheet2_copy.iloc[indexnumber,columnnumber]
2.访问行、行切片
sheet2_copy.iloc[0]
sheet2_copy.iloc[0:2]
3.访问列、列切片
sheet2_copy.iloc[:,0]
sheet2_copy.iloc[:,0:2]
index=['tom','jack','mery','lucy','lili'],
columns=['python','java','php'])
pandas回顾 2 合并与级联
score
|
python |
java |
php |
tom |
44 |
35 |
14 |
jack |
44 |
80 |
10 |
mery |
31 |
97 |
2 |
lucy |
46 |
34 |
31 |
lili |
13 |
69 |
25 |
score.loc['tom','java'] = None
score
|
python |
java |
php |
tom |
44 |
NaN |
14 |
jack |
44 |
80.0 |
10 |
mery |
31 |
97.0 |
2 |
lucy |
46 |
34.0 |
31 |
lili |
13 |
69.0 |
25 |
pandas的聚合操作会自动优化空值,pandas是一种更贴合业务需求的数据结构
score.sum()
结果为:
python 178.0
java 280.0
php 82.0
dtype: float