Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference, connection and usage of .iloc and .loc in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "what is the difference, connection and usage of .iloc and .loc in Python" with detailed content, clear steps and proper handling of details. I hope that this article "what is the difference, connection and usage of .iloc and .loc in Python" can help you solve your doubts.

1. contact

(1) the operands are the same: both loc and iloc operate on DataFrame types.

(2) accomplish the same purpose: both are used to select elements in the corresponding row or column in the DataFrame.

two。 Difference

Loc and iloc indexes have different row and column label types.

Iloc uses sequential numbers to index data, not character labels to index data; note: sequential numbers here refer to counting from 0!

Loc uses the actually set index to index the data. But when the row name is a number, loc can also index the number, but the number here does not necessarily start with 0, it is the number corresponding to the specific column name!

3. Usage

Let's use the code to explain the use of both.

3.1the columns are all numbered import pandas as pdimport numpy as np a = np.arange (12) .reshape (3heli4) # convert a to DataFrame type df = pd.DataFrame (a) # Show dfdf

Since the rows and columns of df are not named, they are numbered from 0 by default, so the result of using loc and iloc at this time is the same.

Index is a number, default output line print (df.loc [0]) # output line 0 element print (df.iloc [0]) # output line 0 element

The output results of both are:

0 0

1 1

2 2

3 3

Name: 0, dtype: int32

The output is the element in row 0 of df, where the first column represents the column name and the second column represents the specific value. If you only need to output a column, enter df.loc [:, 0] to indicate the output column 0.

If you need to output data from columns 0 to 2.

# Mode 1df.loc [:, 0:2] # you can think of loc as a traversal string type, and 0:2 means that the label is 0memery 1meme 2 triple columns # mode 2df.iloc [:, 0:3] # numbers traversed by iloc, and 0:3 in python corresponds to 0meme1 and 2

The output results are as follows:

3.2 there is a row or column that is not numbered from 0 sequentially # replace the row label with another number df.index= [2pd5pd7] df.loc [2]

At this point, df becomes:

The output is as follows:

0 0

1 1

2 2

3 3

Name: 2, dtype: int32

The output corresponds to the row with the column label "2".

Let's continue to use df.iloc [2] to output the results:

0 8

1 9

2 10

3 11

Name: 7, dtype: int32

It can be seen that the output is the second line of data.

Here we can have some understanding of the use of loc and iloc.

3.3.3Row or column non-numeric label # converts the row label to a non-numeric type df.index= ['axiaojiaozhongb'] # outputs rows an and b, columns 0 to 2 data # way 1df.loc [[' axiaqingjianb'], 0:2] # loc can be understood as a traversal string type, and 0:2 indicates that the label is 0primel, ju2, tri-column # mode 2df.iloc [0Rang2Mag0Rang3] # iloc traverses numbers 0:2 means 0 and 1, 0 and 1, 0, 3, 0, 1, 1, 2, 2.

The output results of both are:

3.4 other uses

In general, the behavior of a table goes from a 0-numbered numeric type to a specific string type. The number of the row is easy to determine, and the column name of the column is easy to determine.

# replace the row with the 012 serial number df.index= [0Magne1Phone2] # column label with A B C Ddf.columns= ['Achievement force'] df.iloc [1] ['A'] # to output the data of row 1, column A

The output is 4.

If you want to output row 1, column AB, use df.iloc [1] [['axiajiaojia B']], it is important to note here that' Abecedarian B' is entered as a list, with a total of two brackets on the right.

Output result:

A 4

B 5

Name: 1, dtype: int32

Df.iloc [1] [] is equivalent to df.iloc [1], but in many cases we don't know the number corresponding to the specific column name, so the first method can improve the programming efficiency.

After reading this, the article "what is the difference, connection and usage of .iloc and .loc in Python" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report