溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

pandas中iloc,loc取數(shù)據(jù)有什么區(qū)別

發(fā)布時間:2021-07-26 11:28:54 來源:億速云 閱讀:149 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)pandas中iloc,loc取數(shù)據(jù)有什么區(qū)別,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

Dataframe使用loc取某幾行幾列的數(shù)據(jù):

print(df.loc[0:4,['item_price_level','item_sales_level','item_collected_level','item_pv_level']])

結(jié)果如下,取了index為0到4的五行四列數(shù)據(jù)。

  item_price_level item_sales_level item_collected_level item_pv_level
0     3     3      4    14
1     3     3      4    14
2     3     3      4    14
3     3     3      4    14
4     3     3      4    14

而使用iloc,如下所示:

print(df.iloc[0:4,6:9])

結(jié)果如下,取得是index為0到3四行,以及第6到8列(從0列開始)3列數(shù)據(jù)。

  item_price_level item_sales_level item_collected_level
0     3     3      4
1     3     3      4
2     3     3      4
3     3     3      4

另外loc可以按條件取數(shù)據(jù):

print(df.loc[df.item_price_level==0,:])
print(df.loc[df[item_price_level]==0,:])

上面兩條語句效果是一樣的,都是取item_price_level為0的所有數(shù)據(jù)。可以把冒號改成幾列列名,只取滿足條件的某幾列數(shù)據(jù):

print(df.loc[df['item_price_level']==0,['item_price_level','item_sales_level']])

結(jié)果前兩行如下:

   item_price_level item_sales_level
129141     0    10
129142     0    10

條件為多個時 (同時滿足兩個條件如下):

print(df.loc[(item_price_level==0) & (item_sales_level==3),:])

關(guān)于“pandas中iloc,loc取數(shù)據(jù)有什么區(qū)別”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI