In [20]:data = DataFrame(np.arange(16).reshape((4,4)),
index = ['Ohio','Colorado','Utah','New York'],
columns = ['one','two','three','four'])
In [21]:data
Out[22]:
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 [22]:data.ix['Ohio']
Out[23]:
one 0
two 1
three 2
four 3
Name: Ohio, dtype: int32
In [24]: data
Out[24]:
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 [25]: data.ix['Ohio'][1]
Out[25]: 1
In [26]: data.ix['Ohio'][1]=100
In [27]: data
Out[27]:
one two three four
Ohio 0 100 2 3
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
In [28]: data.ix['Ohio',1]=55
In [29]: data
Out[29]:
one two three four
Ohio 0 55 2 3
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
In [5]: test = DataFrame(np.arange(12.).reshape((4,3)),
...: columns=list('bde'),
...: index = ['Utah','Ohio','Texas','Oregon'])
In [6]: Series = test.b
In [7]: Series
Out[7]:
Utah 0
Ohio 3
Texas 6
Oregon 9
Name: b, dtype: float64
In [8]: test.b-Series
Out[8]:
Utah 0
Ohio 0
Texas 0
Oregon 0
Name: b, dtype: float64
當數據索引不是整數時:
利用標簽切片運算與普通的Python切片運算不同,末端是包含的
In [47]: test
Out[47]:
b d e
Utah 0 1 2
Ohio 3 4 5
Texas 6 7 8
Oregon 9 10 11
In [48]: test[:2]
Out[48]:
b d e
Utah 0 1 2
Ohio 3 4 5
In [49]: test.ix[0:2]
Out[49]:
b d e
Utah 0 1 2
Ohio 3 4 5
In [50]: test[:'Texas']
Out[50]:
b d e
Utah 0 1 2
Ohio 3 4 5
Texas 6 7 8
In [51]: test.ix[:'Texas']
Out[51]:
b d e
Utah 0 1 2
Ohio 3 4 5
Texas 6 7 8
當數據索引不是整數時:
In [52]: df
Out[52]:
0 1 2
0 -1.003236 NaN NaN
1 -2.312056 NaN NaN
2 0.473058 NaN NaN
3 0.716591 NaN 1.066437
4 -0.183461 NaN -0.386878
5 -0.455206 -0.270473 -0.037796
6 -0.232490 -0.208851 -0.077866
In [53]: df[:4]
Out[53]:
0 1 2
0 -1.003236 NaN NaN
1 -2.312056 NaN NaN
2 0.473058 NaN NaN
3 0.716591 NaN 1.066437
In [54]: df.ix[0:4] #索引跟標簽名字一樣時
Out[54]:
0 1 2
0 -1.003236 NaN NaN
1 -2.312056 NaN NaN
2 0.473058 NaN NaN
3 0.716591 NaN 1.066437
4 -0.183461 NaN -0.386878