Series的索引工作方式与Numpy数组索引类似,不同之处是,Series的索引值不只是整数

obj = Series(np.arange(4.), index=['a', 'b', 'c', 'd'])
obj['b']

通过整数选取

obj[1]

切片选取

obj[2:4]

通过索引值集合选取

obj[['b', 'a', 'd']]

通过整数集合选取

obj[[1, 3]]

通过布尔逻辑选取

obj[obj < 2]

通过标签值的切片运算

obj['b':'c']

通过标签值的切片设置值

obj['b':'c'] = 5
obj

a    0.0
b    5.0
c    5.0
d    3.0
dtype: float64

DataFrame索引操作

data = DataFrame(np.arange(16).reshape((4, 4)),
                 index=['Ohio', 'Colorado', 'Utah', 'New York'],
                 columns=['one', 'two', 'three', 'four'])
data

DataFrame选取一个或多个列

data['two']
data[['three', 'one']]

通过切片选取

data[:2]

one    two    three    four
Ohio    0    1    2    3
Colorado    4    5    6    7

通过布尔选取

data[data['three'] > 5]

通过DataFrame的布尔逻辑选取

data[data < 5] = 0

为了在DataFrame的行上使用标签索引

data.ix['Colorado', ['two', 'three']]

two      5
three    6
Name: Colorado, dtype: int64

通过ix选取多行,多列

data.ix[['Colorado', 'Utah'], [3, 0, 1]]

                four    one    two
Colorado    7    0    5
Utah    11    8    9

ix通过整数选取行

data.ix[2]

ix通过标签值选取行与列

data.ix[:'Utah', 'two']

ix混合选取

data.ix[data.three > 5, :3]

                one    two    three
Colorado    0    5    6
Utah    8    9    10
New York    12    13    14

results matching ""

    No results matching ""