1使用iloc
iloc方法
用iloc方法,使用行列的位置对数据框进行切片。支持布尔切片。
行切片
只传入一个参数时,表示对行进行切片。参数为整数返回序列,参数为列表返回数据框。正数表示正向切片, 负数表示反向切片。
选取第一行(序列)
print(df.iloc[0])
Id 1
Name M.S.Dhoni
Age 36
Weight 75
Salary 5428000
Name: 2020-01-01, dtype: object
选取第一行(数据框)
print(df.iloc[[0]])
Id Name Age Weight Salary
2020-01-01 1 M.S.Dhoni 36 75 5428000
选取前2行
print(df.iloc[:2])
Id Name Age Weight Salary
2020-01-01 1 M.S.Dhoni 36 75 5428000
2020-01-02 2 A.B.D Villers 38 74 3428000
选取第三行到行末
print(df.iloc[2:])
Id Name Age Weight Salary
2020-01-03 3 V.Kholi 31 70 8428000
2020-01-04 4 S.Smith 34 80 4428000
2020-01-05 5 C.Gayle 40 100 4528000
2020-01-06 6 J.Root 33 72 7028000
2020-01-07 7 K.Peterson 42 85 2528000
选取1,3,5行(设置起止位置和步长)
print(df.iloc[:6:2])
Id Name Age Weight Salary
2020-01-01 1 M.S.Dhoni 36 75 5428000
2020-01-03 3 V.Kholi 31 70 8428000
2020-01-05 5 C.Gayle 40 100 4528000
选取倒数第2行到行末
print(df.iloc[-2:])
Id Name Age Weight Salary
2020-01-06 6 J.Root 33 72 7028000
2020-01-07 7 K.Peterson 42 85 2528000
选取4,5,6行(布尔列表切片)
print(df.iloc[[False, False, False, True, True, True, False]])
Id Name Age Weight Salary
2020-01-04 4 S.Smith 34 80 4428000
2020-01-05 5 C.Gayle 40 100 4528000
2020-01-06 6 J.Root 33 72 7028000
选取Name字段中包含o字符的行
print(df.iloc[df[‘Name’].str.contains(‘o’).to_list()])
Id Name Age Weight Salary
2020-01-01 1 M.S.Dhoni 36 75 5428000
2020-01-03 3 V.Kholi 31 70 8428000
2020-01-06 6 J.Root 33 72 7028000
2020-01-07 7 K.Peterson 42 85 2528000
列切片
使用iloc方法进行列切片时,需要行参数设置为:,表示选取所有的行。列切片方法与行切片相同。
选取第一列(序列)
print(df.iloc[:, 0])
2020-01-01 1
2020-01-02 2
2020-01-03 3
2020-01-04 4
2020-01-05 5
2020-01-06 6
2020-01-07 7
Name: Id, dtype: int64
选取第一列(数据框)
print(df.iloc[:, [0]])
Id
2020-01-01 1
2020-01-02 2
2020-01-03 3
2020-01-04 4
2020-01-05 5
2020-01-06 6
2020-01-07 7
选取列名中包含a的列
print(df.iloc[:, df.columns.str.contains(‘a’)])
Name Salary
2020-01-01 M.S.Dhoni 5428000
2020-01-02 A.B.D Villers 3428000
2020-01-03 V.Kholi 8428000
2020-01-04 S.Smith 4428000
2020-01-05 C.Gayle 4528000
2020-01-06 J.Root 7028000
2020-01-07 K.Peterson 2528000
组合切片
同时设置行参数与列参数,使用iloc进行组合切片。
选取第一行,第一列的元素
print(df.iloc[0, 0])
1
选取第1,3行,2,4列
print(df.iloc[[0, 2], [1, 3]])
Name Weight
2020-01-01 M.S.Dhoni 75
2020-01-03 V.Kholi 70
选取Name中包含o的行,且列名中包含a的列
print(df.iloc[df[‘Name’].str.contains(‘o’).to_list(), df.columns.str.contains(‘a’)])
Name Salary
2020-01-01 M.S.Dhoni 5428000
2020-01-03 V.Kholi 8428000
2020-01-06 J.Root 7028000
2020-01-07 K.Peterson 2528000
使用loc方法
loc方法
使用loc方法,用行列的名字对数据框进行切片,同时支持布尔索引。
行切片
传入一个参数时,只对行进行切片。
选取索引为2020-01-01的行
print(df.loc[‘2020-01-01’])
Id 1
Name M.S.Dhoni
Age 36
Weight 75
Salary 5428000
Name: 2020-01-01, dtype: object
选取索引为2020-01-01和2020-01-03的行
print(df.loc[[‘2020-01-01’, ‘2020-01-03’]])
Id Name Age Weight Salary
2020-01-01 1 M.S.Dhoni 36 75 5428000
2020-01-03 3 V.Kholi 31 70 8428000
选取Name字段中包含o的行
print(df.loc[df[‘Name’].str.contains(‘o’)])
Id Name Age Weight Salary
2020-01-01 1 M.S.Dhoni 36 75 5428000
2020-01-03 3 V.Kholi 31 70 8428000
2020-01-06 6 J.Root 33 72 7028000
2020-01-07 7 K.Peterson 42 85 2528000
选取索引中包含3或4的行
print(df.loc[df.index.str.contains(‘3|4’)])
Id Name Age Weight Salary
2020-01-03 3 V.Kholi 31 70 8428000
2020-01-04 4 S.Smith 34 80 4428000
列切片
使用loc方法进行列切片时,行参数需要设置为:,表示选取所有行。列切片方法与行切片相同。
选取Name和Age列
print(df.loc[:, [‘Name’, ‘Age’]])
Name Age
2020-01-01 M.S.Dhoni 36
2020-01-02 A.B.D Villers 38
2020-01-03 V.Kholi 31
2020-01-04 S.Smith 34
2020-01-05 C.Gayle 40
2020-01-06 J.Root 33
2020-01-07 K.Peterson 42
选取Name列及后面所有的列
print(df.loc[:, ‘Name’:])
Name Age Weight Salary
2020-01-01 M.S.Dhoni 36 75 5428000
2020-01-02 A.B.D Villers 38 74 3428000
2020-01-03 V.Kholi 31 70 8428000
2020-01-04 S.Smith 34 80 4428000
2020-01-05 C.Gayle 40 100 4528000
2020-01-06 J.Root 33 72 7028000
2020-01-07 K.Peterson 42 85 2528000
选取包含a字符的所有列
print(df.loc[:, df.columns.str.contains(‘a’)])
Name Salary
2020-01-01 M.S.Dhoni 5428000
2020-01-02 A.B.D Villers 3428000
2020-01-03 V.Kholi 8428000
2020-01-04 S.Smith 4428000
2020-01-05 C.Gayle 4528000
2020-01-06 J.Root 7028000
2020-01-07 K.Peterson 2528000
组合切片
同时设置行参数和列参数,使用loc方法进行组合切片。
选取索引为2020-01-01和2020-01-03的行,且列名为Id和Name的列
print(df.loc[[‘2020-01-01’, ‘2020-01-03’], [‘Id’, ‘Name’]])
Id Name
2020-01-01 1 M.S.Dhoni
2020-01-03 3 V.Kholi
选取Name中包含o的行,且列名中包含a的列
print(df.loc[df[‘Name’].str.contains(‘o’), df.columns.str.contains(‘a’)])
Name Salary
2020-01-01 M.S.Dhoni 5428000
2020-01-03 V.Kholi 8428000
2020-01-06 J.Root 7028000
2020-01-07 K.Peterson 2528000
根据索引位置与列名切片
print(df.loc[df.index[3:], [‘Name’, ‘Weight’]])
Name Weight
2020-01-04 S.Smith 80
2020-01-05 C.Gayle 100
2020-01-06 J.Root 72
2020-01-07 K.Peterson 85
根据索引名与列位置切片
print(df.loc[[‘2020-01-01’, ‘2020-01-03’], df.columns[[2, 4]]])
Age Salary
2020-01-01 36 5428000
2020-01-03 31 8428000