pandas+dataframe實現行列選擇與切片操作

來源:互聯網
上載者:User
這次給大家帶來pandas+dataframe實現行列選擇與切片操作,pandas+dataframe實現行列選擇與切片操作的注意事項有哪些,下面就是實戰案例,一起來看一下。

SQL中的select是根據列的名稱來選取;Pandas則更為靈活,不但可根據列名稱選取,還可以根據列所在的position(數字,在第幾行第幾列,注意pandas行列的position是從0開始)選取。相關函數如下:

1)loc,基於列label,可選取特定行(根據行index);

2)iloc,基於行/列的position;

3)at,根據指定行index及列label,快速定位DataFrame的元素;

4)iat,與at類似,不同的是根據position來定位的;

5)ix,為loc與iloc的混合體,既支援label也支援position;

執行個體

import pandas as pdimport numpy as npdf = pd.DataFrame({'total_bill': [16.99, 10.34, 23.68, 23.68, 24.59],          'tip': [1.01, 1.66, 3.50, 3.31, 3.61],          'sex': ['Female', 'Male', 'Male', 'Male', 'Female']})# data type of columnsprint df.dtypes# indexesprint df.index# return pandas.Indexprint df.columns# each row, return array[array]print df.valuesprint df
sex      objecttip      float64total_bill  float64dtype: objectRangeIndex(start=0, stop=5, step=1)Index([u'sex', u'tip', u'total_bill'], dtype='object')[['Female' 1.01 16.99] ['Male' 1.66 10.34] ['Male' 3.5 23.68] ['Male' 3.31 23.68] ['Female' 3.61 24.59]]   sex  tip total_bill0 Female 1.01    16.991  Male 1.66    10.342  Male 3.50    23.683  Male 3.31    23.684 Female 3.61    24.59
print df.loc[1:3, ['total_bill', 'tip']]print df.loc[1:3, 'tip': 'total_bill']print df.iloc[1:3, [1, 2]]print df.iloc[1:3, 1: 3]
  total_bill  tip1    10.34 1.662    23.68 3.503    23.68 3.31  tip total_bill1 1.66    10.342 3.50    23.683 3.31    23.68  tip total_bill1 1.66    10.342 3.50    23.68  tip total_bill1 1.66    10.342 3.50    23.68

錯誤的表示:

print df.loc[1:3, [2, 3]]#.loc僅支援列名操作
KeyError: 'None of [[2, 3]] are in the [columns]'
print df.loc[[2, 3]]#.loc可以不加列名,則是行選擇
  sex  tip total_bill2 Male 3.50    23.683 Male 3.31    23.68
print df.iloc[1:3]#.iloc可以不加第幾列,則是行選擇
sex  tip total_bill1 Male 1.66    10.342 Male 3.50    23.68
print df.iloc[1:3, 'tip': 'total_bill']
TypeError: cannot do slice indexing on <class 'pandas.indexes.base.Index'> with these indexers [tip] of <type 'str'>
print df.at[3, 'tip']print df.iat[3, 1]print df.ix[1:3, [1, 2]]print df.ix[1:3, ['total_bill', 'tip']]
3.313.31  tip total_bill1 1.66    10.342 3.50    23.683 3.31    23.68  total_bill  tip1    10.34 1.662    23.68 3.503    23.68 3.31
print df.ix[[1, 2]]#行選擇
  sex  tip total_bill1 Male 1.66    10.342 Male 3.50    23.68
print df[1: 3]print df[['total_bill', 'tip']]# print df[1:2, ['total_bill', 'tip']] # TypeError: unhashable type
sex  tip total_bill1 Male 1.66    10.342 Male 3.50    23.68  total_bill  tip0    16.99 1.011    10.34 1.662    23.68 3.503    23.68 3.314    24.59 3.61
print df[1:3,1:2]
TypeError: unhashable type

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

pandas中的Dataframe查詢有哪些方法

selenium+cookie跳過驗證碼登入實現步奏詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.