最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pandas中Series和DataFrame的索引實現(xiàn)

 更新時間:2019年06月27日 14:29:52   作者:小舔哥  
這篇文章主要介紹了Pandas中Series和DataFrame的索引實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

正文

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

使用下標(biāo)索引的時候下標(biāo)總是從0開始的,而且索引值總是數(shù)字。而使用關(guān)鍵字進(jìn)行索引,關(guān)鍵字是key里面的值,既可以是數(shù)字,也可以是字符串等。

Series對象介紹:

Series對象是由索引index和值values組成的,一個index對應(yīng)一個value。其中index是pandas中的Index對象。values是numpy中的數(shù)組對象。

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

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

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

如何對Series對象進(jìn)行索引?

1:使用index中的值進(jìn)行索引

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

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


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

2:使用下標(biāo)進(jìn)行索引 

print(s1[0])
結(jié)果:
2

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

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

3:特殊情況:

上面的index為字符串,假如index為數(shù)字,這個時候進(jìn)行索引是按照index值進(jìn)行還是按照下標(biāo)進(jìn)行? 

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

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

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

可以看出來,當(dāng)index為整數(shù)的時候,那么前兩種選擇是使用index的值進(jìn)行索引, 而后一種切片選擇使用的是下標(biāo)進(jìn)行索引。

4:使用布爾Series進(jìn)行索引

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

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

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

5:使用Index對象來進(jìn)行索引

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

DataFrame對象介紹:

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

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)
結(jié)果:
RangeIndex(start=0, stop=4, step=1)

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

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

如何對DataFrame對象進(jìn)行索引

1:使用columns的值對列進(jìn)行索引

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

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

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

2:切片或者布爾Series對行進(jìn)行索引

使用切片索引,或者布爾類型Series進(jìn)行索引:

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

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

3:使用loc和iloc進(jìn)行索引

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

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

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

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

平乐县| 深泽县| 柘城县| 郴州市| 青田县| 甘洛县| 涡阳县| 咸丰县| 大渡口区| 互助| 拜泉县| 上蔡县| 长顺县| 呼伦贝尔市| 滦平县| 衡水市| 武宁县| 高唐县| 抚远县| 康平县| 蛟河市| 藁城市| 页游| 措勤县| 新丰县| 涟水县| 梨树县| 白沙| 田阳县| 克山县| 毕节市| 肇州县| 长沙县| 澄城县| 即墨市| 尖扎县| 金坛市| 长寿区| 五峰| 三都| 达日县|