Several Methods of Extracting Row and Column Data in pandas

  • 2021-08-21 20:54:47
  • OfStack

Several common ways to fetch rows and columns:

data [Column Name]: Single column or multiple columns are fetched. It cannot be fetched in a continuous manner, nor can it be used to fetch rows.
data. Column name: Only used to fetch a single column, not a row.
data [i: j]: Use the starting row subscript (i) and ending row subscript (j) to take a single row or multiple consecutive rows, which cannot be used for column selection.
data. loc [row name, column name]: Use the object's. loc [] method to implement various data fetching methods.
data. iloc [row subscript, column subscript]: Use the. iloc [] method of the object to realize various data fetching methods.

First, generate an DataFrame object:


import pandas as pd
score = [[34,67,87],[68,98,58],[75,73,86],[94,59,81]]
name = [' Xiao Xin ',' Xiao Hong ',' Xiao Li ']
course = [' Language ',' Mathematics ',' English ',' Politics ']
mydata = pd.DataFrame(data=score,columns=name,index=course)# Specify the column name 
print(mydata)

Xiao Xin Xiao Hong Xiao Li
Languages 34 67 87
Mathematics 68 98 58
English 75 73 86
Politics 94 59 81

1. Extract single-column or multi-column data [column name] directly with column name


print(mydata[' Xiao Hong ']) #  The output is 1 A Series Object instead of the DataFrame Object 
 Language  67
 Mathematics  98
 English  73
 Politics  59 

print(mydata[[' Xiao Hong ']]) #  Add [] At this time, the output is DataFrame Object 
  Xiao Hong 
 Language  67
 Mathematics  98
 English  73
 Politics  59

print(mydata[[' Xiao Hong ',' Xiao Li ']]) # Select two columns, at which time you must use the [] Enclose two columns 
  Xiao Hong   Xiao Li 
 Language  67 87
 Mathematics  98 58
 English  73 86
 Politics  59 81

2. Extract one row or multiple consecutive rows of data with the matrix index where the row is located


print(mydata[0:1]) # Pass 0:1 Choose the number 0 Row 
  Xiao Xin   Xiao Hong   Xiao Li 
 Language  34 67 87

mydata[0:3] # Pass 0:3 Choose the number 0,1,23 Row 
  Xiao Xin   Xiao Hong   Xiao Li 
 Language  34 67 87
 Mathematics  68 98 58
 English  75 73 86

3. Obtain a certain column of data in the way of "·" of data


print(mydata. Xiao Hong ) # Pass . Xiaohong chose the little red column, and pay attention to the output Series Object 
 Language  67
 Mathematics  98
 English  73
 Politics  59 

4. Data extraction in panadas using the. loc [,],. iloc [,] methods of the DataFrame object

Citation:


import pandas as pd
score = [[34,67,87],[68,98,58],[75,73,86],[94,59,81]]
name = [' Xiao Xin ',' Xiao Hong ',' Xiao Li ']
course = [' Language ',' Mathematics ',' English ',' Politics ']
mydata1 = pd.DataFrame(data=score,columns=name,index=course) #  Specify the line name ( index ) and column names ( columns ) 
print(mydata1)
mydata2 = pd.DataFrame(score) #  Row name is not specified, and is used by default 0,1,2 ... 
print(mydata2)

Xiao Ming Xiao Hong Xiao Li
Languages 34 67 87
Mathematics 68 98 58
English 75 73 86
Politics 94 59 81
0 1 2
0 34 67 87
1 68 98 58
2 75 73 86
3 94 59 81

Both the. loc [] and. iloc [] methods of the DataFrame object can be used to extract data, except:

. loc []: Takes column and row names as parameters. . iloc []: Take the position index of the 2-dimensional matrix (i.e. 0, 1, 2...) as the parameter.

. loc [] Syntax:

There are two input parameters, the first specifies the row name and the second specifies the column name. When there is only one parameter, the default is the row name (that is, the whole row is extracted), and all columns are selected.

. iloc [] Syntax:

There are two input parameters, the first specifies the row position and the second specifies the column position. When there is only one parameter, the default is the row position (that is, the whole row is extracted), and all columns are selected.

Summary:

When a few columns of all rows need to be selected, row parameters can be omitted and column parameters need to be specified. At this time, column parameters must be preceded by ",:", which is shaped like. loc [:, column parameters] and. iloc [:, column parameters].

When only one input parameter is specified in both methods, it is implicitly related to "row", while "column" is all selected. When both rows and columns need to be specified, it is very important to separate them with "commas", otherwise there will be an error.

Both methods accept two parameters, the first is "row label" or "matrix row number", and the second is "column label" or "matrix column number".

Learning links:

Data extraction in Panadas using. loc [,],. iloc [,] methods of DataFrame object
The DataFrame object of pandas extracts "whole column" or "whole row" data


Related articles: