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
# -*- coding:utf-8 -*-
import pandas as pd

df = pd.read_csv('./iris_training.csv', low_memory=False)
print(df.head(10))
"""    
   120    4  setosa  versicolor  virginica
0  6.4  2.8     5.6         2.2          2
1  5.0  2.3     3.3         1.0          1
2  4.9  2.5     4.5         1.7          2
3  4.9  3.1     1.5         0.1          0
4  5.7  3.8     1.7         0.3          0
5  4.4  3.2     1.3         0.2          0
6  5.4  3.4     1.5         0.4          0
7  6.9  3.1     5.1         2.3          2
8  6.7  3.1     4.4         1.4          1
9  5.1  3.7     1.5         0.4          0"""

行选择

Pandas进行行选择一般有三种方法:

  • 连续多行的选择用类似于python的列表切片
  • loc通过行标签索引来确定行的
  • iloc通过行号索引来确定行
# 第一种,使用类似于python的列表切片
print(df[0:5])
"""
   120    4  setosa  versicolor  virginica
0  6.4  2.8     5.6         2.2          2
1  5.0  2.3     3.3         1.0          1
2  4.9  2.5     4.5         1.7          2
3  4.9  3.1     1.5         0.1          0
4  5.7  3.8     1.7         0.3          0 """


print(df[0:5:2])
""" 
   120    4  setosa  versicolor  virginica
0  6.4  2.8     5.6         2.2          2
2  4.9  2.5     4.5         1.7          2
4  5.7  3.8     1.7         0.3          0 """
# 第二种,按照指定的索引选择一行或多行,使用loc[]方法
# .loc可以不加列名,则是行选择

ser = df.loc[0]
print(ser)
""" 
120           6.4
4             2.8
setosa        5.6
versicolor    2.2
virginica     2.0
Name: 0, dtype: float64 """


maser = df.loc[0:5]   # 包括了5,它与第一种的列表索引最大的不同是包含了索引号为5的那一行数据
print(maser)
""" 
   120    4  setosa  versicolor  virginica
0  6.4  2.8     5.6         2.2          2
1  5.0  2.3     3.3         1.0          1
2  4.9  2.5     4.5         1.7          2
3  4.9  3.1     1.5         0.1          0
4  5.7  3.8     1.7         0.3          0
5  4.4  3.2     1.3         0.2          0 """

print(df.loc[0:5:2])
""" 
   120    4  setosa  versicolor  virginica
0  6.4  2.8     5.6         2.2          2
2  4.9  2.5     4.5         1.7          2
4  5.7  3.8     1.7         0.3          0 """


print(df.loc[[0, 5]])
""" 选择特定的行
   120    4  setosa  versicolor  virginica
0  6.4  2.8     5.6         2.2          2
5  4.4  3.2     1.3         0.2          0 """
# 第三种,按照指定的位置选择一行多多行,使用iloc[]方法
# .iloc可以不加第几列,则是行选择

# 在上面的数据中,使用iloc[]和loc[]的效果是一样的,因为索引号都是从0开始并且连续不断
df2 = df.drop([1,2], axis=0)
print(df2.head(10))
""" 
    120    4  setosa  versicolor  virginica
0   6.4  2.8     5.6         2.2          2
3   4.9  3.1     1.5         0.1          0
4   5.7  3.8     1.7         0.3          0
5   4.4  3.2     1.3         0.2          0
6   5.4  3.4     1.5         0.4          0
7   6.9  3.1     5.1         2.3          2
8   6.7  3.1     4.4         1.4          1
9   5.1  3.7     1.5         0.4          0
10  5.2  2.7     3.9         1.4          1
11  6.9  3.1     4.9         1.5          1 """

print(df2.loc[[0, 1]])
""" 
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative. 

   120    4  setosa  versicolor  virginica
0  6.4  2.8     5.6         2.2        2.0
1  NaN  NaN     NaN         NaN        NaN"""

print(df2.loc[0:5])
""" 
   120    4  setosa  versicolor  virginica
0  6.4  2.8     5.6         2.2          2
3  4.9  3.1     1.5         0.1          0
4  5.7  3.8     1.7         0.3          0
5  4.4  3.2     1.3         0.2          0 """

print(df2.iloc[[0, 1]])
""" 
   120    4  setosa  versicolor  virginica
0  6.4  2.8     5.6         2.2          2
3  4.9  3.1     1.5         0.1          0 """

列选择

# 通过列名选择单列
print(df['120'])
""" 
0      6.4
1      5.0
2      4.9
3      4.9
4      5.7
5      4.4
      ...
115    5.5
116    5.7
117    4.4
118    4.8
119    5.5
Name: 120, Length: 120, dtype: float64"""

# 通过列名选择多列
print(df[['120', 'setosa']])
""" 
     120  setosa
0    6.4     5.6
1    5.0     3.3
2    4.9     4.5
3    4.9     1.5
4    5.7     1.7
5    4.4     1.3
..   ...     ...
115  5.5     4.4
116  5.7     4.2
117  4.4     1.4
118  4.8     1.4
119  5.5     3.7

[120 rows x 2 columns] """

行列选择

# print(df.loc[1:3, [2, 3]]) #.loc仅支持列名操作
# KeyError: 'None of [[2, 3]] are in the [columns]'


print(df.loc[1:3, ['120', 'setosa']])
""" 
   120  setosa
1  5.0     3.3
2  4.9     4.5
3  4.9     1.5 """

print(df.loc[1:3, '120': 'setosa'])
""" 
   120    4  setosa
1  5.0  2.3     3.3
2  4.9  2.5     4.5
3  4.9  3.1     1.5 """

print(df.iloc[1:3, [1, 2]])
""" 
     4  setosa
1  2.3     3.3
2  2.5     4.5 """

print(df.iloc[1:3, 1:3])
""" 
     4  setosa
1  2.3     3.3
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://blog.csdn.net/LY_ysys629/article/details/55224284

https://www.cnblogs.com/kylinlin/p/5231404.html

猜你喜欢

转载自blog.csdn.net/grllery/article/details/81292085
今日推荐