loc and iloc functions in Pandas (extract data from certain columns or rows)

data.iloc[ A:B ,C:D ]

Usage: The front of the comma indicates which rows to take, and the back of the comma indicates which columns to take.

For example 1: data.iloc[ 0:2 ,1:2 ] # Take all the data that intersects rows 0-2 and columns 1-2

For example 2: data.iloc[ : ,1:2 ] # Get all the data that intersects all rows and columns 1-2

For example 3: data.iloc[ : , : ] # Get all data of all rows and all columns

For example 4: data.iloc[ : , [1,2,3] ] # Get all the data that intersects all rows and columns 1, 2, and 3

 

loc function : Get the row data by the specific value in the row index "Index" (for example, take the row whose "Index" is "A" )

iloc function : Get row data by row number ( such as taking the second row of data )

This article gives five common usages of loc and iloc, and attaches detailed codes.

1. Use loc and iloc to extract a row of data

import numpy as np
import pandas as pd
#创建一个Dataframe
data=pd.DataFrame(np.arange(16).reshape(4,4),index=list('abcd'),columns=list('ABCD'))
 
In[1]: data
Out[1]: 
    A   B   C   D
a   0   1   2   3
b   4   5   6   7
c   8   9  10  11
d  12  13  14  15
 
#取索引为'a'的行
In[2]: data.loc['a']
Out[2]:
A    0
B    1
C    2
D    3
 
#取第一行数据,索引为'a'的行就是第一行,所以结果相同
In[3]: data.iloc[0]
Out[3]:
A    0
B    1
C    2
D    3

2. Use loc and iloc to extract a column or several columns of data

In[4]:data.loc[:,['A']] #取'A'列所有行,多取几列格式为 data.loc[:,['A','B']]
Out[4]: 
    A
a   0
b   4
c   8
d  12
 
In[5]:data.iloc[:,[0]] #取第0列所有行,多取几列格式为 data.iloc[:,[0,1]],取第0列和第1列的所有行
Out[5]: 
    A
a   0
b   4
c   8
d  12
 

4. Use loc and iloc to extract all data

In[8]:data.loc[:,:] #取A,B,C,D列的所有行
Out[8]: 
    A   B   C   D
a   0   1   2   3
b   4   5   6   7
c   8   9  10  11
d  12  13  14  15
 
In[9]:data.iloc[:,:] #取第0,1,2,3列的所有行
Out[9]: 
    A   B   C   D
a   0   1   2   3
b   4   5   6   7
c   8   9  10  11
d  12  13  14  15

5. Use the loc function to extract the row where the data is located according to a certain data

In[10]: data.loc[data['A']==0] #提取data数据(筛选条件: A列中数字为0所在的行数据)
Out[10]: 
   A  B  C  D
a  0  1  2  3
 
In[11]: data.loc[(data['A']==0)&(data['B']==2)] #提取data数据(多个筛选条件)
Out[11]: 
   A  B  C  D
a  0  1  2  3

When using the loc function, when the index is the same, all the same Index will be extracted,

The advantage is: if the index is a person's name and the data frame is everyone's data, then I can extract multiple pieces of data from a certain person for analysis;

The disadvantage is: if the index has no specific meaning and is repeated, the extracted data needs to be further processed, and the index can be reset by the .reset_index() function

Here is an actual scenario:

A section in Excel looks like this:

At this point we want to get all the data from the second column to the penultimate column of the Excel table, then I use the following code:

o_train = pd.read_csv('./dataset/train.csv')
o_test = pd.read_csv('./dataset/test.csv')

print(o_train.shape) #(1314, 81)
print(o_test.shape)  #(146, 81)

### 'MSSubClass':'SaleCondition'是第二列到倒数第二列
all_features = pd.concat((o_train.loc[:, 'MSSubClass':'SaleCondition'], o_test.loc[:, 'MSSubClass':'SaleCondition'])) # [1460 rows x 79 columns]

all_labels   = pd.concat((o_train.loc[:, 'SalePrice'], o_test.loc[:, 'SalePrice'])) # Length: 1460,

 Got the following result:

Refer to the following: Detailed explanation of the usage of loc and iloc functions in Pandas (source code + example)

Guess you like

Origin blog.csdn.net/weixin_43135178/article/details/124320736