日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pandas之loc iloc ix

發布時間:2024/4/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pandas之loc iloc ix 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

loc: loc?gets rows (or columns) with particular?labels?from the index. loc從索引中獲取具有特定標簽的行(或列)。這里的關鍵是:標簽。標簽的理解就是name名字。

iloc: gets rows (or columns) at particular?positions?in the index (so it only takes integers).?iloc在索引中的特定位置獲取行(或列)(因此它只接受整數)。這里的關鍵是:位置。位置的理解就是排第幾個。

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一樣的行為。

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是整型數字 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索引多行數據 print(df1.loc[1:]) '''a b c 1 4 5 6 2 7 8 9 '''# loc索引多列數據 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是整型數字 print(df1.iloc[0]) ''' a 1 b 2 c 3 Name: 0, dtype: int64 '''# iloc索引行,label是字符型。如果按照loc的寫法來寫應該是: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是字符型。正確的寫法應該如下: # 也就說,不論index是什么類型的,iloc只能寫位置,也就是整型數字。 print(df2.iloc[0]) ''' a 1 b 2 c 3 Name: e, dtype: int64 '''# iloc索引多行數據 print(df1.iloc[1:]) '''a b c 1 4 5 6 2 7 8 9 '''# iloc索引多列數據 # 如果如下寫法,報錯。 print(df1.iloc[:,['a', 'b']]) ''' TypeError: cannot perform reduce with flexible type '''

# iloc索引多列數據, 正確寫法如下: 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已經不被推薦使用,建議采用iloc和loc實現ix。

(1)如果索引是整數類型,則ix將僅使用基于標簽的索引,而不會回退到基于位置的索引。如果標簽不在索引中,則會引發錯誤。

(2)如果索引不僅包含整數,則給定一個整數,ix將立即使用基于位置的索引而不是基于標簽的索引。但是,如果ix被賦予另一種類型(例如字符串),則它可以使用基于標簽的索引。

接下來舉例說明這兩個特點:

>>> s = pd.Series(np.nan, index=[49,48,47,46,45, 1, 2, 3, 4, 5]) >>> s 49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN 2 NaN 3 NaN 4 NaN 5 NaN

現在我們來看使用整數3切片有什么結果:

在這個例子中,s.iloc[:3]讀取前3行(因為iloc把3看成是位置position),而s.loc[:3]讀取的是前8行(因為loc把3看作是索引的標簽label)

>>> s.iloc[:3] # slice the first three rows 49 NaN 48 NaN 47 NaN>>> s.loc[:3] # slice up to and including label 3 49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN 2 NaN 3 NaN>>> s.ix[:3] # the integer is in the index so s.ix[:3] works like loc 49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN 2 NaN 3 NaN

注意:s.ix[:3]返回的結果與s.loc[:3]一樣,這是因為如果series的索引是整型的話,ix會首先去尋找索引中的標簽3而不是去找位置3。

如果,我們試圖去找一個不在索引中的標簽,比如說是6呢?

>>> s.iloc[:6] 49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN>>> s.loc[:6] KeyError: 6>>> s.ix[:6] KeyError: 6

在上面的例子中,s.iloc[:6]正如我們所期望的,返回了前6行。而,s.loc[:6]返回了KeyError錯誤,這是因為標簽6并不在索引中。

那么,s.ix[:6]報錯的原因是什么呢?正如我們在ix的特點1所說的那樣,如果索引只有整數類型,那么ix僅使用基于標簽的索引,而不會回退到基于位置的索引。如果標簽不在索引中,則會引發錯誤。

如果我們的索引是一個混合的類型,即不僅僅包括整型,也包括其他類型,如字符類型。那么,給ix一個整型數字,ix會立即使用iloc操作,而不是報KeyError錯誤。

>>> s2 = pd.Series(np.nan, index=['a','b','c','d','e', 1, 2, 3, 4, 5]) >>> s2.index.is_mixed() # index is mix of different types True >>> s2.ix[:6] # now behaves like iloc given integer a NaN b NaN c NaN d NaN e NaN 1 NaN

注意:在這種情況下,ix也可以接受非整型,這樣就是loc的操作:

>>> s2.ix[:'c'] # behaves like loc given non-integer a NaN b NaN c NaN

這個例子就說明了ix特點2。

正如前面所介紹的,ix的使用有些復雜。如果僅使用位置或者標簽進行切片,使用iloc或者loc就行了,請避免使用ix。

4.在Dataframe中使用ix實現復雜切片

有時候,在使用Dataframe進行切片時,我們想混合使用標簽和位置來對行和列進行切片。那么,應該怎么操作呢?

舉例,考慮有下述例子中的Dataframe。我們想得到直到包含標簽'c'的行和前4列。

>>> df = pd.DataFrame(np.nan, index=list('abcde'),columns=['x','y','z', 8, 9]) >>> dfx y z 8 9 a NaN NaN NaN NaN NaN b NaN NaN NaN NaN NaN c NaN NaN NaN NaN NaN d NaN NaN NaN NaN NaN e NaN NaN NaN NaN NaN

在pandas的早期版本(0.20.0)之前,ix可以很好地實現這個功能。

我們可以使用標簽來切分行,使用位置來切分列(請注意:因為4并不是列的名字,因為ix在列上是使用的iloc)。

>>> df.ix[:'c', :4]x y z 8 a NaN NaN NaN NaN b NaN NaN NaN NaN c NaN NaN NaN NaN

在pandas的后來版本中,我們可以使用iloc和其它的一個方法就可以實現上述功能:

>>> df.iloc[:df.index.get_loc('c') + 1, :4]x y z 8 a NaN NaN NaN NaN b NaN NaN NaN NaN c NaN NaN NaN NaN

get_loc()?是得到標簽在索引中的位置的方法。請注意,因為使用iloc切片時不包括最后1個點,因為我們必須加1。

可以看到,只使用iloc更好用,因為不必理會ix的那2個“繁瑣”的特點。

參考文獻:https://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation-how-are-they-different

轉載于https://blog.csdn.net/anshuai_aw1/article/details/82801435

轉載于:https://www.cnblogs.com/RB26DETT/p/11557339.html

總結

以上是生活随笔為你收集整理的pandas之loc iloc ix的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: av图片在线观看 | 黑人玩弄人妻一区二区三区四 | 中文字幕亚洲精品在线观看 | 国产素人在线观看 | 激情五月激情综合网 | 免费观看黄色网页 | 欧美永久| 国产在线一区二区 | 久久国产精品影院 | 俺来也俺也啪www色 欧洲一区二区视频 | 欧美激情福利 | 久久久久久国产精品三区 | 免费日韩网站 | 日本一区二区三区四区在线观看 | 好吊视频在线观看 | 影音先锋在线播放 | 亚洲高清不卡 | 麻豆传媒网站 | 欧美性猛交ⅹ乱大交3 | 蜜臀av一区二区三区有限公司 | 欧美三级韩国三级日本三斤 | 欧美一区二区三区的 | 色播基地| 爱上av| 欧美性受xxx黑人xyx性爽 | 97人妻精品一区二区免费 | 狂躁美女大bbbbbb黑人 | 韩国三级一区 | 香蕉视频最新网址 | 天堂网在线最新版www中文网 | 日韩成人av一区 | 亚洲女人天堂成人av在线 | 成年人的视频网站 | 小视频在线播放 | 国产精品久久毛片av大全日韩 | 福利社区一区二区 | 久久大综合 | 欧美福利视频一区 | 亚洲女女做受ⅹxx高潮 | 波多野结衣1区2区3区 | 91蝌蚪在线观看 | 国产中文字幕在线视频 | 亚洲国产中文字幕在线 | 色一情一交一乱一区二区三区 | 俺去日 | 99精品热| 第一章婶婶的性事 | 中文字幕精品久久久久人妻红杏ⅰ | 日韩一区二区毛片 | 免费在线观看黄视频 | 午夜在线观看视频18 | 日本亚洲最大的色成网站www | 国产麻豆一区二区三区在线观看 | 亚洲第一色网 | 日韩av成人 | 中文字幕乱妇无码av在线 | 日本wwwwww| av大全在线观看 | 亚洲视频欧洲视频 | 激情文学欧美 | 97超视频在线观看 | 国语对白清晰刺激对白 | 午夜视频在线观看国产 | 99热这里只有精品8 国产一卡二 | 黄色小视频网 | 欧洲xxxxx| 五月婷婷六月综合 | 粉嫩欧美一区二区三区 | 精品女厕偷拍一区二区 | 亚洲一区欧洲二区 | 欧美韩日一区二区 | 泰坦尼克号3小时49分的观看方法 | 久久精品无码Av中文字幕 | 曰韩精品| 99久久精品无码一区二区毛片 | 美女又爽又黄免费 | 干极品美女 | 久久久久99精品成人片毛片 | 日本男人天堂 | 免费福利影院 | 久久精品一区二区在线观看 | 免费看黄色一级视频 | 午夜av片 | 国内偷拍精品视频 | 中文字幕av无码一区二区三区 | 欧美一级片一区二区 | 国产精品久久久久久久av | 亚洲一区二区三区播放 | 国产91绿帽单男绿奴 | 琪琪原网址 | 911av| 黄色av网站在线播放 | 在线免费国产视频 | 日韩精选在线观看 | 欧美一区二区三区色 | 艳母免费在线观看 | 日本aⅴ在线 | 五月丁香 | 午夜鲁鲁 |