pandas的DataFrame的行列选择

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;

The simplified rules of indexing are

  • 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进行行选择一般有三种方法:

  • 连续多行的选择用类似于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]…则进行的是行选择

2).loc,.at,选列是只能是列名,不能是position

3).iloc,.iat,选列是只能是position,不能是列名

4)df[]只能进行行选择,或列选择,不能同时进行列选择,列选择只能是列名。

原文地址:https://www.cnblogs.com/liangzaikaituozhe/p/10337286.html