3.3 数据取值与选择
Series
将Series看作字典
import pandas as pd
data = pd.Series([0.25, 0.5, 0.75, 1], index=['a', 'b', 'c', 'd'])
data
a 0.25
b 0.50
c 0.75
d 1.00
dtype: float64
# 键引用
data['b']
0.5
'a' in data
True
data.keys()
Index(['a', 'b', 'c', 'd'], dtype='object')
list(data.items())
[('a', 0.25), ('b', 0.5), ('c', 0.75), ('d', 1.0)]
# 用字典方式添加新数据元素
data['e'] = 1.25
data
a 0.25
b 0.50
c 0.75
d 1.00
e 1.25
dtype: float64
将Series看作一维数组
# 将显式索引作为切片,最后的索引也会切片进来
data['a':'c']
a 0.25
b 0.50
c 0.75
dtype: float64
# 将隐式整数索引作为切片
data[0:2]
a 0.25
b 0.50
dtype: float64
# 掩码
data[(data > 0.3) & (data < 0.8)]
b 0.50
c 0.75
dtype: float64
# 花哨的索引
data[['a', 'e']]
a 0.25
e 1.25
dtype: float64
将显式索引作为切片,最后的索引也会切片进来。如果显式的索引本身也是数字,就会和隐式数字索引混淆,因此有索引器方法。
data = pd.Series(['a', 'b', 'c'], index=[1, 3, 5])
data
1 a
3 b
5 c
dtype: object
# 取值操作是显式索引
data[1]
'a'
# 切片操作是隐式索引,左闭右开的python式
data[1:3]
3 b
5 c
dtype: object
# 索引器1 loc属性,取值和切片都是显式
data.loc[1]
'a'
data.loc[1:3]
1 a
3 b
dtype: object
# 索引器2 iloc属性,取值和切片都是隐式,左闭右开的python式
data.iloc[1]
'b'
data.iloc[1:3]
3 b
5 c
dtype: object
DataFrame
将DF看作字典,由若干Series对象构成的字典
area_dict = {'e': 50, 'b': 46, 'c': 66, 'd': 211}
area = pd.Series(area_dict)
popu_dict={'e': 6622, 'b': 5644, 'c': 9022, 'd': 1222111}
popu = pd.Series(popu_dict)
data = pd.DataFrame({'area': area, 'popu': popu})
data
|
area |
popu |
e |
50 |
6622 |
b |
46 |
5644 |
c |
66 |
9022 |
d |
211 |
1222111 |
# 两个Series分别构成DF的一列
# 可用列名进行字典式取值获取数据
data['area']
e 50
b 46
c 66
d 211
Name: area, dtype: int64
# 也可用属性形式选择纯字符串列名的数据
data.area
e 50
b 46
c 66
d 211
Name: area, dtype: int64
# 两种方式结果相同
data.area is data['area']
True
属性方式的局限性:列名不是字符串,或列名与内置属性相同,则不能用
# 可用字典形式的语法调整对象,如添加一列
data['density'] = data['popu']/data['area']
data
|
area |
popu |
density |
e |
50 |
6622 |
132.440000 |
b |
46 |
5644 |
122.695652 |
c |
66 |
9022 |
136.696970 |
d |
211 |
1222111 |
5791.995261 |
将DF看作二维数组
# 用value属性查看数据
data.values
array([[5.00000000e+01, 6.62200000e+03, 1.32440000e+02],
[4.60000000e+01, 5.64400000e+03, 1.22695652e+02],
[6.60000000e+01, 9.02200000e+03, 1.36696970e+02],
[2.11000000e+02, 1.22211100e+06, 5.79199526e+03]])
# 获得行列转置
data.T
|
e |
b |
c |
d |
area |
50.00 |
46.000000 |
66.00000 |
2.110000e+02 |
popu |
6622.00 |
5644.000000 |
9022.00000 |
1.222111e+06 |
density |
132.44 |
122.695652 |
136.69697 |
5.791995e+03 |
data
|
area |
popu |
density |
e |
50 |
6622 |
132.440000 |
b |
46 |
5644 |
122.695652 |
c |
66 |
9022 |
136.696970 |
d |
211 |
1222111 |
5791.995261 |
# 用索引器取值
# iloc索引器,python式前闭后开取值,索引为隐式数字
data.iloc[:3, :2]
|
area |
popu |
e |
50 |
6622 |
b |
46 |
5644 |
c |
66 |
9022 |
# loc索引器,前闭后闭取值,索引为显式字符串
data.loc[:'b', :'popu']
|
area |
popu |
e |
50 |
6622 |
b |
46 |
5644 |
# ix索引器,为混合效果,易混淆,不建议使用
data.ix[:3, :'popu']
c:\program files\python36-32\lib\site-packages\ipykernel_launcher.py:2: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
|
area |
popu |
e |
50 |
6622 |
b |
46 |
5644 |
c |
66 |
9022 |
# 任何用于np数据的方法都可用于索引器,如结合掩码和花哨
data.loc[data.density > 130, ['popu', 'density']]
|
popu |
density |
e |
6622 |
132.440000 |
c |
9022 |
136.696970 |
d |
1222111 |
5791.995261 |
# 任何取值方法都可以修改数据,与np的方法相同
data.iloc[0, 2] = 90
data
|
area |
popu |
density |
e |
50 |
6622 |
90.000000 |
b |
46 |
5644 |
122.695652 |
c |
66 |
9022 |
136.696970 |
d |
211 |
1222111 |
5791.995261 |
其他取值方法
# 单个标签取值,就获得列,要用列标签
data['area']
e 50
b 46
c 66
d 211
Name: area, dtype: int64
# 多个标签取值,就获得行,要用行标签
data['b':'d']
|
area |
popu |
density |
b |
46 |
5644 |
122.695652 |
c |
66 |
9022 |
136.696970 |
d |
211 |
1222111 |
5791.995261 |
# 切片也可以不用索引值,直接用行数。前闭后开。
data[1:3]
|
area |
popu |
density |
b |
46 |
5644 |
122.695652 |
c |
66 |
9022 |
136.696970 |
# 掩码操作也可以直接对每行过滤,而不用loc索引器
data[data.density > 130]
|
area |
popu |
density |
c |
66 |
9022 |
136.696970 |
d |
211 |
1222111 |
5791.995261 |