内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Pandas中iloc、loc、ix三者的區(qū)別是什么-創(chuàng)新互聯(lián)

這篇文章給大家介紹Pandas中iloc、loc、ix三者的區(qū)別是什么,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)富陽,十載網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220

1. iloc和loc的區(qū)別:

iloc主要使用數(shù)字來索引數(shù)據(jù),而不能使用字符型的標簽來索引數(shù)據(jù)。而loc則剛好相反,只能使用字符型標簽來索引數(shù)據(jù),不能使用數(shù)字來索引數(shù)據(jù),不過有特殊情況,當數(shù)據(jù)框dataframe的行標簽或者列標簽為數(shù)字,loc就可以來其來索引。

好,先上代碼,先上行標簽和列標簽都為數(shù)字的情況。

import pandas as pd
import numpy as np
a = np.arange(12).reshape(3,4)
print a
>>>
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]
df = pd.DataFrame(a)
print df
>>>
 0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
print df.loc[0]
>>>
0 0
1 1
2 2
3 3
Name: 0, dtype: int32
print df.iloc[0]
0 0
1 1
2 2
3 3
Name: 0, dtype: int32
print df.loc[:,[0,3]]
 0 3
0 0 3
1 4 7
2 8 11
print df.iloc[:,[0,3]]
 0 3
0 0 3
1 4 7
2 8 11

接下來是把行標簽[0, 1, 2]改成['a', 'b', 'c'],則成這樣了。

df.index = ['a','b','c'] 
print df 
>>> 
 0 1 2 3 
a 0 1 2 3 
b 4 5 6 7 
c 8 9 10 11 
print df.loc[0] 
# TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <type 'int'> 
print df.iloc[0] 
>>> 
0 0 
1 1 
2 2 
3 3 
Name: a, dtype: int32 
print df.iloc['a'] # TypeError: cannot do positional indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [a] of <type 'str'> 
print df.loc['a'] # 正確 
>>> 
0 0 
1 1 
2 2 
3 3 
Name: a, dtype: int32

同樣地,把列標簽[0, 1, 2, 3]改成['A', 'B, 'C', 'D'],則成這樣了。

df.columns = ['A','B','C','D']
print df
>>>
 A B C D
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
print df.loc[:,'A']
>>>
a 0
b 4
c 8
Name: A, dtype: int32
print df.iloc[:,'A'] # ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types

2.ix是一種混合索引,字符型標簽和整型數(shù)據(jù)索引都可以。

print df.ix[0]
>>>
A 0
B 1
C 2
D 3
Name: a, dtype: int32
print df.ix['a']
>>>
A 0
B 1
C 2
D 3
Name: a, dtype: int32
print df.ix[:,0]
>>>
a 0
b 4
c 8
Name: A, dtype: int32
print df.ix[:,'A']
>>>
a 0
b 4
c 8
Name: A, dtype: int32

關(guān)于Pandas中iloc、loc、ix三者的區(qū)別是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

標題名稱:Pandas中iloc、loc、ix三者的區(qū)別是什么-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://www.rwnh.cn/article16/popdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、虛擬主機、域名注冊、網(wǎng)站制作微信小程序、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化
龙山县| 乌拉特前旗| 满洲里市| 香格里拉县| 中阳县| 莎车县| 镇坪县| 松滋市| 叙永县| 菏泽市| 清原| 肥乡县| 鄂温| 双鸭山市| 黑龙江省| 巴马| 望奎县| 安化县| 云龙县| 张家界市| 会东县| 哈巴河县| 聊城市| 阜新| 富阳市| 罗江县| 德州市| 东光县| 肃南| 东源县| 黑河市| 日喀则市| 曲周县| 成都市| 茂名市| 漳州市| 新和县| 平江县| 邛崃市| 方山县| 渭南市|