pandas的DataFrame的行列選擇

Pandas可根據列名稱選取,還能夠根據列所在的position(數字,在第幾行第幾列,注意pandas行列的position是從0開始)選取。相關函數以下:python

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

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

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

4)iat,與at相似,不一樣的是根據position來定位的;索引

5)ix,爲loc與iloc的混合體,既支持label也支持position;utf-8

The simplified rules of indexing areget

  • Use loc for label-based indexing
  • Use iloc for positional indexing
  1. # -*- coding:utf-8 -*-
  2. import pandas as pd
  3.  
  4. df = pd.read_csv( './iris_training.csv', low_memory=False)
  5. print(df.head( 10))
  6. """
  7. 120 4 setosa versicolor virginica
  8. 0 6.4 2.8 5.6 2.2 2
  9. 1 5.0 2.3 3.3 1.0 1
  10. 2 4.9 2.5 4.5 1.7 2
  11. 3 4.9 3.1 1.5 0.1 0
  12. 4 5.7 3.8 1.7 0.3 0
  13. 5 4.4 3.2 1.3 0.2 0
  14. 6 5.4 3.4 1.5 0.4 0
  15. 7 6.9 3.1 5.1 2.3 2
  16. 8 6.7 3.1 4.4 1.4 1
  17. 9 5.1 3.7 1.5 0.4 0"""

行選擇

Pandas進行行選擇通常有三種方法:string

  • 連續多行的選擇用相似於python的列表切片
  • loc經過行標籤索引來肯定行的
  • iloc經過行號索引來肯定行
  1. # 第一種,使用相似於python的列表切片
  2. print(df[ 0:5])
  3. """
  4. 120 4 setosa versicolor virginica
  5. 0 6.4 2.8 5.6 2.2 2
  6. 1 5.0 2.3 3.3 1.0 1
  7. 2 4.9 2.5 4.5 1.7 2
  8. 3 4.9 3.1 1.5 0.1 0
  9. 4 5.7 3.8 1.7 0.3 0 """
  10.  
  11.  
  12. print(df[ 0:5:2])
  13. """
  14. 120 4 setosa versicolor virginica
  15. 0 6.4 2.8 5.6 2.2 2
  16. 2 4.9 2.5 4.5 1.7 2
  17. 4 5.7 3.8 1.7 0.3 0 """
  1. # 第二種,按照指定的索引選擇一行或多行,使用loc[]方法
  2. # .loc能夠不加列名,則是行選擇
  3.  
  4. ser = df.loc[ 0]
  5. print(ser)
  6. """
  7. 120 6.4
  8. 4 2.8
  9. setosa 5.6
  10. versicolor 2.2
  11. virginica 2.0
  12. Name: 0, dtype: float64 """
  13.  
  14.  
  15. maser = df.loc[ 0:5] # 包括了5,它與第一種的列表索引最大的不一樣是包含了索引號爲5的那一行數據
  16. print(maser)
  17. """
  18. 120 4 setosa versicolor virginica
  19. 0 6.4 2.8 5.6 2.2 2
  20. 1 5.0 2.3 3.3 1.0 1
  21. 2 4.9 2.5 4.5 1.7 2
  22. 3 4.9 3.1 1.5 0.1 0
  23. 4 5.7 3.8 1.7 0.3 0
  24. 5 4.4 3.2 1.3 0.2 0 """
  25.  
  26. print(df.loc[ 0:5:2])
  27. """
  28. 120 4 setosa versicolor virginica
  29. 0 6.4 2.8 5.6 2.2 2
  30. 2 4.9 2.5 4.5 1.7 2
  31. 4 5.7 3.8 1.7 0.3 0 """
  32.  
  33.  
  34. print(df.loc[[ 0, 5]])
  35. """ 選擇特定的行
  36. 120 4 setosa versicolor virginica
  37. 0 6.4 2.8 5.6 2.2 2
  38. 5 4.4 3.2 1.3 0.2 0 """
  1. # 第三種,按照指定的位置選擇一行多多行,使用iloc[]方法
  2. # .iloc能夠不加第幾列,則是行選擇
  3.  
  4. # 在上面的數據中,使用iloc[]和loc[]的效果是同樣的,由於索引號都是從0開始而且接二連三
  5. df2 = df.drop([ 1,2], axis=0)
  6. print(df2.head( 10))
  7. """
  8. 120 4 setosa versicolor virginica
  9. 0 6.4 2.8 5.6 2.2 2
  10. 3 4.9 3.1 1.5 0.1 0
  11. 4 5.7 3.8 1.7 0.3 0
  12. 5 4.4 3.2 1.3 0.2 0
  13. 6 5.4 3.4 1.5 0.4 0
  14. 7 6.9 3.1 5.1 2.3 2
  15. 8 6.7 3.1 4.4 1.4 1
  16. 9 5.1 3.7 1.5 0.4 0
  17. 10 5.2 2.7 3.9 1.4 1
  18. 11 6.9 3.1 4.9 1.5 1 """
  19.  
  20. print(df2.loc[[ 0, 1]])
  21. """
  22. Passing list-likes to .loc or [] with any missing label will raise
  23. KeyError in the future, you can use .reindex() as an alternative.
  24.  
  25. 120 4 setosa versicolor virginica
  26. 0 6.4 2.8 5.6 2.2 2.0
  27. 1 NaN NaN NaN NaN NaN"""
  28.  
  29. print(df2.loc[ 0:5])
  30. """
  31. 120 4 setosa versicolor virginica
  32. 0 6.4 2.8 5.6 2.2 2
  33. 3 4.9 3.1 1.5 0.1 0
  34. 4 5.7 3.8 1.7 0.3 0
  35. 5 4.4 3.2 1.3 0.2 0 """
  36.  
  37. print(df2.iloc[[ 0, 1]])
  38. """
  39. 120 4 setosa versicolor virginica
  40. 0 6.4 2.8 5.6 2.2 2
  41. 3 4.9 3.1 1.5 0.1 0 """

列選擇

  1. # 經過列名選擇單列
  2. print(df[ '120'])
  3. """
  4. 0 6.4
  5. 1 5.0
  6. 2 4.9
  7. 3 4.9
  8. 4 5.7
  9. 5 4.4
  10. ...
  11. 115 5.5
  12. 116 5.7
  13. 117 4.4
  14. 118 4.8
  15. 119 5.5
  16. Name: 120, Length: 120, dtype: float64"""
  17.  
  18. # 經過列名選擇多列
  19. print(df[[ '120', 'setosa']])
  20. """
  21. 120 setosa
  22. 0 6.4 5.6
  23. 1 5.0 3.3
  24. 2 4.9 4.5
  25. 3 4.9 1.5
  26. 4 5.7 1.7
  27. 5 4.4 1.3
  28. .. ... ...
  29. 115 5.5 4.4
  30. 116 5.7 4.2
  31. 117 4.4 1.4
  32. 118 4.8 1.4
  33. 119 5.5 3.7
  34.  
  35. [120 rows x 2 columns] """
  36.  
  37. # 若是沒有列名
  38. # df[df.columns[0]]

行列選擇

  1. # print(df.loc[1:3, [2, 3]]) #.loc僅支持列名操做
  2. # KeyError: 'None of [[2, 3]] are in the [columns]'
  3.  
  4.  
  5. print(df.loc[ 1:3, ['120', 'setosa']])
  6. """
  7. 120 setosa
  8. 1 5.0 3.3
  9. 2 4.9 4.5
  10. 3 4.9 1.5 """
  11.  
  12. print(df.loc[ 1:3, '120': 'setosa'])
  13. """
  14. 120 4 setosa
  15. 1 5.0 2.3 3.3
  16. 2 4.9 2.5 4.5
  17. 3 4.9 3.1 1.5 """
  18.  
  19. print(df.iloc[ 1:3, [1, 2]])
  20. """
  21. 4 setosa
  22. 1 2.3 3.3
  23. 2 2.5 4.5 """
  24.  
  25. print(df.iloc[ 1:3, 1:3])
  26. """
  27. 4 setosa
  28. 1 2.3 3.3
  29. 2 2.5 4.5 """

總結

1).loc,.iloc,.ix,只加第一個參數如.loc([1,2]),.iloc([2:3]),.ix[2]…則進行的是行選擇pandas

2).loc,.at,選列是隻能是列名,不能是positionit

3).iloc,.iat,選列是隻能是position,不能是列名

4)df[]只能進行行選擇,或列選擇,不能同時進行列選擇,列選擇只能是列名。

相關文章
相關標籤/搜索