Pandas中Series和DataFrame的索引實(shí)現(xiàn)
正文
在對(duì)Series對(duì)象和DataFrame對(duì)象進(jìn)行索引的時(shí)候要明確這么一個(gè)概念:是使用下標(biāo)進(jìn)行索引,還是使用關(guān)鍵字進(jìn)行索引。比如list進(jìn)行索引的時(shí)候使用的是下標(biāo),而dict索引的時(shí)候使用的是關(guān)鍵字。
使用下標(biāo)索引的時(shí)候下標(biāo)總是從0開始的,而且索引值總是數(shù)字。而使用關(guān)鍵字進(jìn)行索引,關(guān)鍵字是key里面的值,既可以是數(shù)字,也可以是字符串等。
Series對(duì)象介紹:
Series對(duì)象是由索引index和值values組成的,一個(gè)index對(duì)應(yīng)一個(gè)value。其中index是pandas中的Index對(duì)象。values是numpy中的數(shù)組對(duì)象。
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]
如何對(duì)Series對(duì)象進(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é)果(注意,切片索引保存最后一個(gè)值): 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é)果(注意:這里和上面不同的是不保存最后一個(gè)值,與正常索引相同): b 3 c 4 dtype: int64
3:特殊情況:
上面的index為字符串,假如index為數(shù)字,這個(gè)時(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]) 會(huì)報(bào)錯(cuò) 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ù)的時(shí)候,那么前兩種選擇是使用index的值進(jìn)行索引, 而后一種切片選擇使用的是下標(biāo)進(jìn)行索引。
4:使用布爾Series進(jìn)行索引
使用布爾Series進(jìn)行索引的時(shí)候,其實(shí)是要求布爾Series和我們的索引對(duì)象有相同的index。
s1 = pd.Series([2,3,4,5], index=['a', 'b', 'c', 'd'] print(s1 > 3) 結(jié)果(這是一個(gè)bool Series): a False b False c True d True dtype: bool print(s1[s1 > 3]) 結(jié)果(只需要把bool Series 傳入Series就可以實(shí)現(xiàn)索引): c 4 d 5 dtype: int64
5:使用Index對(duì)象來進(jìn)行索引
使用Index對(duì)象進(jìn)行索引的時(shí)候,和使用值索引沒有本質(zhì)的區(qū)別。因?yàn)镮ndex里面也存入了很多值,可以把Index看做一個(gè)list。
DataFrame對(duì)象介紹:
DataFrame對(duì)象是一個(gè)由行列組成的表。DataFrame中行由columns組成,列由index組成,它們都是Index對(duì)象。它的值還是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]]
如何對(duì)DataFrame對(duì)象進(jìn)行索引
1:使用columns的值對(duì)列進(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)對(duì)列進(jìn)行索引,除非該columns當(dāng)中包含該值。如下面的操作是錯(cuò)誤的
print(df1[0])
結(jié)果: 錯(cuò)誤
2:切片或者布爾Series對(duì)行進(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)行索引,其實(shí)還是要求布爾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是不理會(huì)index和columns當(dāng)中的值的,永遠(yuǎn)都是用從0開始的下標(biāo)進(jìn)行索引。所以當(dāng)你搞懂這句話的時(shí)候,下面的索引就會(huì)變得非常簡(jiǎn)單:
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
keras在構(gòu)建LSTM模型時(shí)對(duì)變長(zhǎng)序列的處理操作
這篇文章主要介紹了keras在構(gòu)建LSTM模型時(shí)對(duì)變長(zhǎng)序列的處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06跟老齊學(xué)Python之大話題小函數(shù)(1)
今天本講要講什么呢?今天要介紹幾個(gè)python中的小函數(shù),這幾個(gè)函數(shù)都是從函數(shù)式編程借鑒過來的,它們就是:filter、map、reduce、lambda、yield 有了它們,最大的好處是程序更簡(jiǎn)潔2014-10-10Python學(xué)習(xí)筆記之Break和Continue用法分析
這篇文章主要介紹了Python學(xué)習(xí)筆記之Break和Continue用法,結(jié)合實(shí)例形式分析了Python中Break和Continue的功能、使用方法、區(qū)別及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-08-08Python?第三方庫?Pandas?數(shù)據(jù)分析教程
這篇文章主要介紹了Python?第三方庫?Pandas?數(shù)據(jù)分析教程的相關(guān)資料,需要的朋友可以參考下2022-09-09Python實(shí)現(xiàn)字符串中某個(gè)字母的替代功能
小編想實(shí)現(xiàn)這樣一個(gè)功能:將輸入字符串中的字母 “i” 變成字母 “p”。想著很簡(jiǎn)單,怎么實(shí)現(xiàn)呢?下面小編給大家?guī)砹薖ython實(shí)現(xiàn)字符串中某個(gè)字母的替代功能,感興趣的朋友一起看看吧2019-10-10Python tkinter實(shí)現(xiàn)春節(jié)煙花效果demo
這篇文章主要為大家介紹了Python實(shí)現(xiàn)春節(jié)煙花效果demo,本文為大家提供了兩種實(shí)現(xiàn)方式代碼,詳細(xì)的實(shí)現(xiàn)一場(chǎng)浪漫的煙花秀,有需要的朋友可以借鑒參考下2024-01-01Pandas時(shí)間序列:時(shí)期(period)及其算術(shù)運(yùn)算詳解
今天小編就為大家分享一篇Pandas時(shí)間序列:時(shí)期(period)及其算術(shù)運(yùn)算詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02