一、numpy
1. 一維numpy數組的索引
arr=np.arange(10)
arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr[5] # 數組名[索引號]
5
# 切片索引 數組名[起始索引號:終止索引號] 不包括終止索引號
arr[5:8]
array([5, 6, 7])
arr[5:8]=12 #直接修改原數組
arr
array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
2. 二維numpy數組的索引
arr2=np.array([[1,2,3],[4,5,6],[7,8,9]])
arr2
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
arr2[2] #訪問索引以(2)開頭的值,包括(2,0),(2,1),(2,2)表示某一行的值
array([3, 6, 9])
arr2[:1]#訪問數組的某幾行
array([[1, 2, 3]])
arr2[0][2] #前邊表示行,後邊表示列,可以訪問具體的某個值與arr2[0,2]效果一樣
3
arr2[:1,:2] #,前邊表示行,後邊表示列,可以訪問具體的某個值和一個範圍的值
array([[1, 2]])
3. 布爾型索引
names=np.array(['Bob','Joe','Will','Bob','Will','Joe','Joe'])
data=np.random.randn(7,4)
names
array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], dtype='<U4')
data
array([[-0.29702806, 0.16529614, -0.72684132, 1.34725395],
[-0.42805177, -1.26251365, -0.18073154, -0.49129737],
[ 0.29208283, 0.36520126, 0.62905828, 1.00058399],
[ 0.34855322, 0.28424986, 0.07021468, -0.11893882],
[ 0.69535789, -0.11105581, -1.44341613, 1.45839566],
[-0.08256376, 0.49573743, -0.9002769 , -2.49175582],
[ 0.14072487, 1.57377442, 0.95774634, 0.04137535]])
names=='Bob' #假設每個名字對應data數組中的一行,想選中對應於Bob的所有行。要保證比爾型數組長度與被索引的長度一致
array([ True, False, False, True, False, False, False])
data[names=='Bob']
array([[0. , 0.16529614, 0. , 1.34725395],
[0.34855322, 0.28424986, 0.07021468, 0. ]])
data[data<0]=0 #data<0結果是一個布爾型數組
data
array([[0. , 0.16529614, 0. , 1.34725395],
[0. , 0. , 0. , 0. ],
[0.29208283, 0.36520126, 0.62905828, 1.00058399],
[0.34855322, 0.28424986, 0.07021468, 0. ],
[0.69535789, 0. , 0. , 1.45839566],
[0. , 0.49573743, 0. , 0. ],
[0.14072487, 1.57377442, 0.95774634, 0.04137535]])
二、Series
1. 索引
obj=pd.Series(np.arange(4),index=['a','b','c','d'])
obj
a 0
b 1
c 2
d 3
dtype: int32
obj[1] #對象名[索引的序號]
1
obj['b'] #對象名[索引名]
1
obj[['c','b']] #同時添加多個索引名時要把所有索引名放在一個列表中
b 1
c 2
dtype: int32
2.切片
obj[0:2] #索引數字切片,不包含右邊界
a 0
b 1
dtype: int32
obj['b':'c'] #注:c,b不可以調換位置,否則查不出數據。左端的標籤必須在右端的標籤前邊
b 1
c 2
dtype: int32
三、DataFrame
一、索引
數據:
import pandas as pd
data=pd.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
索引操作
用值或者序列對DataFrame進行索引獲取一個或多個列
data['two']
Ohio 1
Colorado 5
Utah 9
New York 13
Name: two, dtype: int32
data[['two','one']]
two one
Ohio 1 0
Colorado 5 4
Utah 9 8
New York 13 12
選取行
data[:2]
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
data[data['three']>5]#根據值選擇
one two three four
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
用loc (軸標籤)和iloc(整數索引)進行選取
data.loc['Colorado':'Utah','one':'three']
# 標籤切片,包含左右邊界
one two three
Colorado 4 5 6
Utah 8 9 10
data.iloc[2,[3,0,1]]
#整數切片,用iloc和整數選取,不包含右邊界
four 11
one 8
two 9
Name: Utah, dtype: int32
data.iloc[[1,2],[3,0,1]]
four one two
Colorado 7 4 5
Utah 11 8 9
data.iloc[:,:3][data.three>5]
#data.three>5爲布爾型索引
one two three
Colorado 4 5 6
Utah 8 9 10
New York 12 13 14
整數索引都不包含右邊界
同樣的Series對象使用三種索引的區別
ser=pd.Series(np.arange(5))
ser
0 0
1 1
2 2
3 3
4 4
dtype: int32
ser[2] #得到的是值
2
ser.loc[2:2]#返回的Series對象,不包括右邊界
2 2
dtype: int32
ser.iloc[2:3]#返回的Series對象,包括右邊界
2 2
dtype: int32