Usage of iloc and loc in python

Usage of iloc and loc in python

Recently, I learned the slice usage of loc and iloc, and found that there are many usages, so I used a simple example to summarize the usage, and also borrowed a lot of notes during the period. If there are mistakes, I look forward to corrections from friends in the comment area.
Pandas retrieves the value of a column in a dictionary-like manner.
The distribution of the data data.csv is as follows:
insert image description here
view the data:

import pandas as pd

data = pd.read_csv('./data.csv',index_col=0,encoding='gb2312')
print(data)
print(data.shape)
index = data.index
col = data.columns
print(index)
print(col)

The result is as follows:

   A   B   C   D   E   F
a   1   2   3   4   5   6
b   7   8   9  10  11  12
c  13  14  15  16  17  18
d  19  20  21  22  23  24
e  25  26  27  28  29  30
f  31  32  33  34  35  36
g  37  38  39  40  41  42
h  43  44  45  46  47  48
(8, 6)
Index(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], dtype='object')
Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')

It can be seen that index is the column index label, and columns is the row index label. At this time, the matrix is ​​a matrix of 8*6.

loc: label index

That is, row and column labels are used for indexing
. In order to facilitate understanding, we will match the examples given by loc and iloc one by one. If there are friends who do not understand, welcome to interact in the comment area.

  1. When we want to get the values ​​from the first row to the second row, the first column to the third column:
x = data.loc['a':'b','A':'C']
print(x)

The result is as follows:

   A  B  C
a  1  2  3
b  7  8  9
  1. When we need to get the data of all columns in the first two rows:
x = data.loc['a':'b',:]
print(x)

The result is as follows:

   A  B  C   D   E   F
a  1  2  3   4   5   6
b  7  8  9  10  11  12
  1. When we need to get the data of all rows from the second column to the third column:
x = data.loc[:,'B':'C']
print(x)

The result is as follows:

    B   C
a   2   3
b   8   9
c  14  15
d  20  21
e  26  27
f  32  33
g  38  39
h  44  45
  1. If you want to index interval rows and columns:
    for example, get the values ​​in the second and fourth rows and in the first and fourth columns
x=data.loc[['b','d'],['A','D']]
print(x)

The result is as follows:

    A   D
b   7  10
d  19  22

5. Index according to the conditions, for example, get the values ​​in column A that are greater than 19 and are in the third and fifth columns:

x=data.loc[data['A'] > 19, ['C', 'E']]
print(x)

The result is as follows:

    C   E
e  27  29
f  33  35
g  39  41
h  45  47

iloc: location index

  1. When we want to get the values ​​from the first row to the second row, the first column to the third column:
x = data.iloc[0:2,0:3]
print(x)

The result is as follows:

   A  B  C
a  1  2  3
b  7  8  9
  1. When we need to get the data of all columns in the first two rows:
x = data.iloc[0:2,:]
print(x)

The result is as follows:

   A  B  C   D   E   F
a  1  2  3   4   5   6
b  7  8  9  10  11  12
  1. When we need to get the data of all rows from the second column to the third column:
x = data.iloc[:,1:3]
print(x)

The result is as follows:

    B   C
a   2   3
b   8   9
c  14  15
d  20  21
e  26  27
f  32  33
g  38  39
h  44  45
  1. If you want to index interval rows and columns:
    for example, get the values ​​in the second and fourth rows and in the first and fourth columns
x=data.iloc[[1,3],[0,3]]
print(x)

The result is as follows:

    A   D
b   7  10
d  19  22

5. Index according to the conditions, for example, get the values ​​in column A that are greater than 19 and are in the third and fifth columns:

x=data.iloc[(data['A'] > 19).values, [2, 4]]
print(x)

The result is as follows:

    C   E
e  27  29
f  33  35
g  39  41
h  45  47

Summarize:So we can summarize the usage of loc and iloc, data.loc[row label to be indexed, column label to be indexed] , data.iloc[row to be indexed, column to be indexed]

Guess you like

Origin blog.csdn.net/qq_45699150/article/details/123590426