How to use the df.iloc [] method of pandas in python
It is believed that many inexperienced people have no idea about how to use the df.iloc [] method of pandas in python. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Unlike df.loc [], which acquires data based on row or column labels, df.iloc [] is obtained according to the coordinates (position) of the data, as identified by the red number in the following figure:
Iloc [] also accepts two parameters, which represent row coordinates and column coordinates. Acceptable parameters
Types are numbers, lists of numeric types, and slices
The following examples are given:
Name score grade
Id
A bog 45 A
C jiken 67 B
D bob 23 A
B jiken 34 B
F lucy 98 A
E tidy 75 B
# get data with coordinates (0,0)
> df.iloc [0,0]
'bog'
# get all the data in 0 rows. The column coordinate parameter is empty. The whole row is obtained by default.
> df.iloc [0]
Name bog
Score 45
Grade A
Name: a, dtype: object
# get the data of the specified row and column
> df.iloc [[0jue 1], [0Jet 1]]
Name score
Id
A bog 45
C jiken 67
# arrange all rows in reverse order
> df.iloc [::-1,::-1]
Grade score name
Id
E B 75 tidy
F A 98 lucy
B B 34 jiken
D A 23 bob
C B 67 jiken
An A 45 bog
After reading the above, have you mastered how to use the df.iloc [] method of pandas in python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!