pandas.DataFrame.iloc()
纯基于位置的整数索引
输入格式:
一个整数列表或数组,如[4,3,0]。
一个带有int类型的slice对象,例如1:7。
一个布尔值数组。
一个具有一个参数的可调用函数,返回索引
Case
mydict = [{
'a': 1, 'b': 2, 'c': 3, 'd': 4},
{
'a': 100, 'b': 200, 'c': 300, 'd': 400},
{
'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]
df = pd.DataFrame(mydict)
df
a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000
Index the rows
An integer-return series format
type(df.iloc[0])
<class 'pandas.core.series.Series'>
df.iloc[0]
a 1
b 2
c 3
d 4
Name: 0, dtype: int64
A list of integers-returns in DataFrame format
df.iloc[[0]]
a b c d
0 1 2 3 4
type(df.iloc[[0]])
<class 'pandas.core.frame.DataFrame'>
df.iloc[[0, 1]]
a b c d
0 1 2 3 4
1 100 200 300 400
slice
df.iloc[:3]
a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000
Boolean type
df.iloc[[True, False, True]]
a b c d
0 1 2 3 4
2 1000 2000 3000 4000
Callable function
df.iloc[lambda x: x.index % 2 == 0]
a b c d
0 1 2 3 4
2 1000 2000 3000 4000
Index two axes
Integer index
df.iloc[0, 1]
2
List index
df.iloc[[0, 2], [1, 3]]
b d
0 2 4
2 2000 4000
Slice index
df.iloc[1:3, 0:3]
a b c
1 100 200 300
2 1000 2000 3000
Boolean index
df.iloc[:, [True, False, True, False]]
a c
0 1 3
1 100 300
2 1000 3000
Callable function index
df.iloc[:, lambda df: [0, 2]]
a c
0 1 3
1 100 300
2 1000 3000