from pandas import Series
import numpy as np
obj = Series(np.arange(4),index=['a','b','c','d'])
obj
a 0
b 1
c 2
d 3
dtype: int64
obj['b']
1
obj[1]
1
obj[2:4]
c 2
d 3
dtype: int64
obj[['b','a','d']]
b 1
a 0
d 3
dtype: int64
obj[[1,3]]
b 1
d 3
dtype: int64
obj[obj <2]
a 0
b 1
dtype: int64
DataFrame
索引列
from pandas import DataFrame
data = DataFrame(np.arange(16).reshape((4,4,)),
index=['Ohio','Colorado','Utah','New York'],columns=['one','two','three','four'])
data
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
data['two']
Ohio 1
Colorado 5
Utah 9
New York 13
Name: two, dtype: int64
data[['three','one']]
three
one
Ohio
2
0
Colorado
6
4
Utah
10
8
New York
14
12
索引行
data[:2]# 返回行
one
two
three
four
Ohio
0
1
2
3
Colorado
4
5
6
7
data['three']>5
Ohio False
Colorado True
Utah True
New York True
Name: three, dtype: bool