来源:《利用Python进行数据分析·第2版》
In [133]: obj = pd.Series(np.arange(4.), index=['a', 'b', 'c', 'd'])
In [134]: obj
Out[134]:
a 0.0
b 1.0
c 2.0
d 3.0
dtype: float64
In [135]: obj['b']
Out[135]: 1.0
In [136]: obj[1]
Out[136]: 1.0
In [137]: obj[2:4]
Out[137]:
c 2.0
d 3.0
dtype: float64
In [138]: obj[['b', 'a', 'd']]
Out[138]:
b 1.0
a 0.0
d 3.0
dtype: float64
In [139]: obj[[1, 3]]
Out[139]:
b 1.0
d 3.0
dtype: float64
In [140]: obj[obj<2]
Out[140]:
a 0.0
b 1.0
dtype: float64
利用标签的切片运算与普通的Python切片运算不同,其末端是包含的:
In [141]: obj['b':'c']
Out[141]:
b 1.0
c 2.0
dtype: float64
用切片可以对Series的相应部分进行设置:
In [142]: obj['b':'c'] = 5
In [143]: obj
Out[143]:
a 0.0
b 5.0
c 5.0
d 3.0
dtype: float64
用一个值或序列对DataFrame进行索引其实就是获取一个或多个列:
In [144]: data = pd.DataFrame(np.arange(16).reshape((4, 4)),
...: index=['Ohio', 'Colorado', 'Utah', 'New York'],
...: columns=['one', 'two', 'three', 'four'])
In [145]: data
Out[145]:
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
In [147]: data['two']
Out[147]:
Ohio 1
Colorado 5
Utah 9
New York 13
Name: two, dtype: int32
In [148]: data[['three', 'one']]
Out[148]:
three one
Ohio 2 0
Colorado 6 4
Utah 10 8
New York 14 12
这种索引方式有几个特殊的情况。首先通过切片或布尔型数组选取数据:
In [149]: data[:2]
Out[149]:
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
In [150]: data[data['three'] > 5]
Out[150]:
one two three four
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
选取行的语法data[:2]十分方便。向[ ]传递单一的元素或列表,就可选择列。
另一种用法是通过布尔型DataFrame(比如下面这个由标量比较运算得出的)进行索引:
In [151]: data < 5
Out[151]:
one two three four
Ohio True True True True
Colorado True False False False
Utah False False False False
New York False False False False
In [152]: data[data < 5] = 0
In [153]: data
Out[153]:
one two three four
Ohio 0 0 0 0
Colorado 0 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
用loc和iloc进行选取
对于DataFrame的行的标签索引,我引入了特殊的标签运算符loc和iloc。它们可以让你用类似NumPy的标记,使用轴标签(loc)或整数索引(iloc),从DataFrame选择行和列的子集。
作为一个初步示例,让我们通过标签选择一行和多列:
In [154]: data.loc['Colorado', ['two', 'three']]
Out[154]:
two 5
three 6
Name: Colorado, dtype: int32
然后用iloc和整数进行选取:
In [156]: data.iloc[2, [3, 0, 1]]
Out[156]:
four 11
one 8
two 9
Name: Utah, dtype: int32
In [157]: data.iloc[2]
Out[157]:
one 8
two 9
three 10
four 11
Name: Utah, dtype: int32
In [158]: data.iloc[[1, 2], [3, 0, 1]]
Out[158]:
four one two
Colorado 7 0 5
Utah 11 8 9
这两个索引函数也适用于一个标签或多个标签的切片:
In [160]: data.loc[:'Utah', "two"]
Out[160]:
Ohio 0
Colorado 5
Utah 9
Name: two, dtype: int32
In [161]: data.iloc[:, :3][data.three>5]
Out[161]:
one two three
Colorado 0 5 6
Utah 8 9 10
New York 12 13 14
所以,在pandas中,有多个方法可以选取和重新组合数据。对于DataFrame,表5-4进行了总结。后面会看到,还有更多的方法进行层级化索引。
整数索引
处理整数索引的pandas对象常常难住新手,因为它与Python内置的列表和元组的索引语法不同。例如,你可能不认为下面的代码会出错:
In [162]: ser = pd.Series(np.arange(3.))
In [163]: ser
Out[163]:
0 0.0
1 1.0
2 2.0
dtype: float64
In [164]: ser[-1]
---------------------------------------------------------------------------
KeyError
对于非整数索引,不会产生歧义:
In [165]: In [145]: ser2 = pd.Series(np.arange(3.), index=['a', 'b', 'c'])
...:
In [166]: ser2
Out[166]:
a 0.0
b 1.0
c 2.0
dtype: float64
In [167]: ser2[-1]
Out[167]: 2.0
为了进行统一,如果轴索引含有整数,数据选取总会使用标签。为了更准确,请使用loc(标签)或iloc(整数):
In [168]: ser[:1]
Out[168]:
0 0.0
dtype: float64
In [169]: ser.loc[:1]
Out[169]:
0 0.0
1 1.0
dtype: float64
In [170]: ser.iloc[:1]
Out[170]:
0 0.0
dtype: float64