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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python pandas dataframe 行列选择,切片操作 原创 2017年02月15日 21:43:18 标签: python 30760 python pandas dataframe

發布時間:2025/3/21 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python pandas dataframe 行列选择,切片操作 原创 2017年02月15日 21:43:18 标签: python 30760 python pandas dataframe 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python pandas dataframe 行列選擇,切片操作

原創 2017年02月15日 21:43:18
  • 30760
  • 編輯
  • 刪除

python 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 pd import 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 columns print df.dtypes # indexes print df.index # return pandas.Index print df.columns # each row, return array[array] print df.values print df
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
sex object tip float64 total_bill float64 dtype: object RangeIndex(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_bill 0 Female 1.01 16.99 1 Male 1.66 10.34 2 Male 3.50 23.68 3 Male 3.31 23.68 4 Female 3.61 24.59
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
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]
  • 1
  • 2
  • 3
  • 4
total_bill tip 1 10.34 1.66 2 23.68 3.50 3 23.68 3.31tip total_bill 1 1.66 10.34 2 3.50 23.68 3 3.31 23.68tip total_bill 1 1.66 10.34 2 3.50 23.68tip total_bill 1 1.66 10.34 2 3.50 23.68
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

錯誤的表示:

print df.loc[1:3, [2, 3]]#.loc僅支持列名操作
  • 1
KeyError: 'None of [[2, 3]] are in the [columns]'
  • 1
  • 2
print df.loc[[2, 3]]#.loc可以不加列名,則是行選擇
  • 1
sex tip total_bill 2 Male 3.50 23.68 3 Male 3.31 23.68
  • 1
  • 2
  • 3
  • 4
print df.iloc[1:3]#.iloc可以不加第幾列,則是行選擇
  • 1
sex tip total_bill 1 Male 1.66 10.34 2 Male 3.50 23.68
  • 1
  • 2
  • 3
  • 4
print df.iloc[1:3, 'tip': 'total_bill']
  • 1
TypeError: cannot do slice indexing on <class 'pandas.indexes.base.Index'> with these indexers [tip] of <type 'str'>
  • 1
  • 2
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']]
  • 1
  • 2
  • 3
  • 4
3.31 3.31tip total_bill 1 1.66 10.34 2 3.50 23.68 3 3.31 23.68total_bill tip 1 10.34 1.66 2 23.68 3.50 3 23.68 3.31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
print df.ix[[1, 2]]#行選擇
  • 1
sex tip total_bill 1 Male 1.66 10.34 2 Male 3.50 23.68
  • 1
  • 2
  • 3
  • 4
print df[1: 3] print df[['total_bill', 'tip']] # print df[1:2, ['total_bill', 'tip']] # TypeError: unhashable type
  • 1
  • 2
  • 3
sex tip total_bill 1 Male 1.66 10.34 2 Male 3.50 23.68total_bill tip 0 16.99 1.01 1 10.34 1.66 2 23.68 3.50 3 23.68 3.31 4 24.59 3.61
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
print df[1:3,1:2]
  • 1
TypeError: unhashable type
  • 1
  • 2

總結

1).loc,.iloc,.ix,只加第一個參數如.loc([1,2]),.iloc([2:3]),.ix[2]…則進行的是行選擇
2).loc,.at,選列是只能是列名,不能是position
3).iloc,.iat,選列是只能是position,不能是列名
4)df[]只能進行行選擇,或列選擇,不能同時進行列選擇,列選擇只能是列名。

總結

以上是生活随笔為你收集整理的python pandas dataframe 行列选择,切片操作 原创 2017年02月15日 21:43:18 标签: python 30760 python pandas dataframe的全部內容,希望文章能夠幫你解決所遇到的問題。

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