SoFunction
Updated on 2024-11-19

Explaining the typical usage of pandas [] in detail

Unlike [], which fetches data based on row or column labels, [] fetches data based on its position, as indicated by the red numbers in the figure below:

iloc[] also accepts two arguments, representing row coordinates and column coordinates. Acceptable parameters

Types of numbers, lists of number types, and slices

Examples are given below:

     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 the data with coordinates (0, 0)
>>> [0, 0]
'bog'

# Get all the data in row 0, the column coordinate parameter is empty, the default is to get the whole line
>>> [0]
name     bog
score     45
grade      A
Name: a, dtype: object

# Get the data for the specified row and column
>>> [[0,1], [0, 1]]
     name  score
id              
a     bog     45
c   jiken     67

# Sort all rows and columns in reverse order
>>> [::-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
a      A     45    bog

to this article on the typical use of pandas [] is introduced to this article, more related pandas [] usage content please search my previous posts or continue to browse the following related articles I hope that you will support me in the future!