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

Dataframe使用loc取某幾行幾列的數據:
print(df.loc[0:4,['item_price_level','item_sales_level','item_collected_level','item_pv_level']])
結果如下,取了index為0到4的五行四列數據。
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])
結果如下,取得是index為0到3四行,以及第6到8列(從0列開始)3列數據。
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可以按條件取數據:
print(df.loc[df.item_price_level==0,:]) print(df.loc[df[item_price_level]==0,:])
上面兩條語句效果是一樣的,都是取item_price_level為0的所有數據??梢园衙疤柛某蓭琢辛忻?,只取滿足條件的某幾列數據:
print(df.loc[df['item_price_level']==0,['item_price_level','item_sales_level']])
結果前兩行如下:
item_price_level item_sales_level 129141 0 10 129142 0 10
條件為多個時 (同時滿足兩個條件如下):
print(df.loc[(item_price_level==0) & (item_sales_level==3),:])
關于“pandas中iloc,loc取數據有什么區別”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
網頁題目:pandas中iloc,loc取數據有什么區別-創新互聯
網頁URL:http://www.yijiale78.com/article0/cspsoo.html
成都網站建設公司_創新互聯,為您提供軟件開發、用戶體驗、全網營銷推廣、網站導航、網站設計公司、做網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯