Pandas中Series和DataFrame的索引


 

在對Series對象和DataFrame對象進行索引的時候要明確這么一個概念:是使用下標進行索引,還是使用關鍵字進行索引。比如list進行索引的時候使用的是下標,而dict索引的時候使用的是關鍵字。

使用下標索引的時候下標總是從0開始的,而且索引值總是數字。而使用關鍵字進行索引,關鍵字是key里面的值,既可以是數字,也可以是字符串等。

 

Series對象介紹:

  Series對象是由索引index和值values組成的,一個index對應一個value。其中index是pandas中的Index對象。values是numpy中的數組對象。

import pandas as pd
s1 = pd.Series([2,3,4,5], index=['a', 'b', 'c', 'd'])
print(s1)
結果:
a    2
b    3
c    4
d    5
dtype: int64

print(s1.index)
結果:
Index(['a', 'b', 'c', 'd'], dtype='object')

print(s1.values)
結果:
[2 3 4 5]

 

如何對Series對象進行索引?

1:使用index中的值進行索引

print(s1['a'])
結果:
2

print(s1[['a','d']])
結果:
a    2
d    5
dtype: int64


print(s1['b':'d'])
結果(注意,切片索引保存最后一個值):
b    3
c    4
d    5
dtype: int64

2:使用下標進行索引

print(s1[0])
結果:
2

print(s1[[0,3]])
結果:
a    2
d    5
dtype: int64

print(s1[1:3])
結果(注意:這里和上面不同的是不保存最后一個值,與正常索引相同):
b    3
c    4
dtype: int64

3:特殊情況:

上面的index為字符串,假如index為數字,這個時候進行索引是按照index值進行還是按照下標進行?

s1 = pd.Series([2,3,4,5], index=[1,2,3,4])
print(s1[2])
結果:
3
print(s1[0]) 會報錯

print(s1[[2,4]])
結果:
2    3
4    5
dtype: int64

print(s1[1:3])
結果:
2    3
3    4
dtype: int64

可以看出來,當index為整數的時候,那么前兩種選擇是使用index的值進行索引, 而后一種切片選擇使用的是下標進行索引。

4:使用布爾Series進行索引

使用布爾Series進行索引的時候,其實是要求布爾Series和我們的索引對象有相同的index。

s1 = pd.Series([2,3,4,5], index=['a', 'b', 'c', 'd']
print(s1 > 3)
結果(這是一個bool Series):
a    False
b    False
c     True
d     True
dtype: bool

print(s1[s1 > 3])
結果(只需要把bool Series 傳入Series就可以實現索引):
c    4
d    5
dtype: int64

5:使用Index對象來進行索引

  使用Index對象進行索引的時候,和使用值索引沒有本質的區別。因為Index里面也存入了很多值,可以把Index看做一個list。

DataFrame對象介紹:

  DataFrame對象是一個由行列組成的表。DataFrame中行由columns組成,列由index組成,它們都是Index對象。它的值還是numpy數組。

data = {'name':['ming', 'hong', 'gang', 'tian'], 'age':[12, 13, 14, 20], 'score':[80.3, 88.2, 90, 99.9]}
df1 = pd.DataFrame(data)

print(df1.index)
結果:
RangeIndex(start=0, stop=4, step=1)

print(df1.columns)
結果:
Index(['age', 'name', 'score'], dtype='object')

print(df1.values)
結果:
[[12 'ming' 80.3]
 [13 'hong' 88.2]
 [14 'gang' 90.0]
 [20 'tian' 99.9]]

如何對DataFrame對象進行索引

1:使用columns的值對列進行索引

  直接使用columns中的值進行索引,得到的是一列或者是多列的值

print(df1['name'])
結果:
0    ming
1    hong
2    gang
3    tian
Name: name, dtype: object

print(df1[['name','age']])
結果:
name  age
0  ming   12
1  hong   13
2  gang   14
3  tian   20
 
         
注意:不可以直接使用下標對列進行索引,除非該columns當中包含該值。如下面的操作是錯誤的
print(df1[0])
結果: 錯誤
 
        

2:切片或者布爾Series對行進行索引

使用切片索引,或者布爾類型Series進行索引:

print(df1[0:3])
使用切片進行選擇,結果:
age  name  score
0   12  ming   80.3
1   13  hong   88.2
2   14  gang   90.0

print(df1[ df1['age'] > 13 ])
使用布爾類型Series進行索引,其實還是要求布爾Series和DataFrame有相同的index,結果: age name score 2   14  gang   90.0
3   20  tian   99.9
 
        

3:使用loc和iloc進行索引

本質上loc是用index和columns當中的值進行索引,而iloc是不理會index和columns當中的值的,永遠都是用從0開始的下標進行索引。所以當你搞懂這句話的時候,下面的索引就會變得非常簡單:

print(df1.loc[3])
結果:
name     hong
score    88.2
Name: 3, dtype: object

print(df1.loc[:,'age'])
結果:
1    12
3    13
4    14
5    20
Name: age, dtype: int64

print(df1.iloc[3])
結果:
age        20
name     tian
score    99.9
Name: 5, dtype: object

print(df1.iloc[:,1])
結果:
1    ming
3    hong
4    gang
5    tian
Name: name, dtype: object


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM