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

詳解pandas中iloc, loc和ix的區(qū)別和聯(lián)系

 更新時間:2020年03月09日 15:28:27   作者:anshuai_aw1  
這篇文章主要介紹了詳解pandas中iloc, loc和ix的區(qū)別和聯(lián)系,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

Pandas庫十分強大,但是對于切片操作iloc, loc和ix,很多人對此十分迷惑,因此本篇博客利用例子來說明這3者之一的區(qū)別和聯(lián)系,尤其是iloc和loc。

對于ix,由于其操作有些復雜,我在另外一篇博客專門詳細介紹ix。

首先,介紹這三種方法的概述:

  • loc gets rows (or columns) with particular labels from the index. loc從索引中獲取具有特定標簽的行(或列)。這里的關(guān)鍵是:標簽。標簽的理解就是name名字。
  • iloc gets rows (or columns) at particular positions in the index (so it only takes integers). iloc在索引中的特定位置獲取行(或列)(因此它只接受整數(shù))。這里的關(guān)鍵是:位置。位置的理解就是排第幾個。
  • ix usually tries to behave like loc but falls back to behaving like iloc if a label is not present in the index. ix通常會嘗試像loc一樣行為,但如果索引中不存在標簽,則會退回到像iloc一樣的行為。(這句話有些繞口,沒關(guān)系,不明白可以看這里)

接下來,舉幾個例子說明:

1 loc

其實,對于loc始終堅持一個原則:loc是基于label進行索引的!

import pandas as pd
df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns=['a','b','c'])
df2 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c'])
print(df1)
print(df2)
'''
df1:
  a b c
0 1 2 3
1 4 5 6
2 7 8 9
df2:
  a b c
e 1 2 3
f 4 5 6
g 7 8 9
'''
 
# loc索引行,label是整型數(shù)字
print(df1.loc[0])
'''
a  1
b  2
c  3
Name: 0, dtype: int64
'''
 
# loc索引行,label是字符型
print(df2.loc['e'])
'''
a  1
b  2
c  3
Name: 0, dtype: int64
'''
# 如果對df2這么寫:df2.loc[0]會報錯,因為loc索引的是label,顯然在df2的行的名字中沒有叫0的。
print(df2.loc[0])
'''
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'>
'''
 
# loc索引多行數(shù)據(jù)
print(df1.loc[1:])
'''
  a b c
1 4 5 6
2 7 8 9
'''
 
# loc索引多列數(shù)據(jù)
print(df1.loc[:,['a', 'b']])
'''
  a b
0 1 2
1 4 5
2 7 8
'''
# df1.loc[:,0:2]這么寫報錯, 因為loc索引的是label,顯然在df1的列的名字中沒有叫0,1和2的。
print(df1.loc[:,0:2])
'''
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'>
'''
 
# locs索引某些行某些列
print(df1.loc[0:2, ['a', 'b']])
'''
  a b
0 1 2
1 4 5
2 7 8
'''

2 iloc

其實,對于iloc始終堅持一個原則:iloc是基于position進行索引的!

import pandas as pd
df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns=['a','b','c'])
df2 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c'])
print(df1)
print(df2)
'''
df1:
  a b c
0 1 2 3
1 4 5 6
2 7 8 9
df2:
  a b c
e 1 2 3
f 4 5 6
g 7 8 9
'''
# iloc索引行,label是整型數(shù)字
print(df1.iloc[0])
'''
a  1
b  2
c  3
Name: 0, dtype: int64
'''
 
# iloc索引行,label是字符型。如果按照loc的寫法來寫應(yīng)該是:df2.iloc['e'],顯然這樣報錯,因為iloc不認識label,它是基于位置的。
print(df2.iloc['e'])
'''
TypeError: cannot do positional indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [e] of <class 'str'>
'''
# iloc索引行,label是字符型。正確的寫法應(yīng)該如下:
# 也就說,不論index是什么類型的,iloc只能寫位置,也就是整型數(shù)字。
print(df2.iloc[0])
'''
a  1
b  2
c  3
Name: e, dtype: int64
'''
 
# iloc索引多行數(shù)據(jù)
print(df1.iloc[1:])
'''
  a b c
1 4 5 6
2 7 8 9
'''
 
# iloc索引多列數(shù)據(jù)
# 如果如下寫法,報錯。
print(df1.iloc[:,['a', 'b']])
'''
TypeError: cannot perform reduce with flexible type
'''
# iloc索引多列數(shù)據(jù), 正確寫法如下:
print(df1.iloc[:,0:2])
'''
  a b
0 1 2
1 4 5
2 7 8
'''
 
# iloc索引某些行某些列
print(df1.iloc[0:2, 0:1])
'''
  a
0 1
1 4
'''

3 ix

ix的操作比較復雜,在pandas版本0.20.0及其以后版本中,ix已經(jīng)不被推薦使用,建議采用iloc和loc實現(xiàn)ix。

如有對ix的使用比較感興趣的朋友可以參考這篇博客。

到此這篇關(guān)于詳解pandas中iloc, loc和ix的區(qū)別和聯(lián)系的文章就介紹到這了,更多相關(guān)pandas iloc loc ix內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

洪湖市| 保德县| 金门县| 松滋市| 武威市| 庄浪县| 峡江县| 曲水县| 中西区| 咸阳市| 绥滨县| 炉霍县| 冀州市| 利津县| 张掖市| 凤翔县| 柯坪县| 邓州市| 拉萨市| 乐平市| 卢氏县| 九龙县| 烟台市| 虎林市| 家居| 永清县| 嵊泗县| 蕉岭县| 河源市| 寿宁县| 佛坪县| 壤塘县| 乌恰县| 同江市| 昌平区| 长宁县| 利津县| 海淀区| 得荣县| 枣强县| 运城市|