SoFunction
Updated on 2024-10-29

Python Pandas data analysis iloc and loc usage details

Pandas is a set of fast and efficient data analysis tools for Python. It can be used for data mining and data analysis, and also provides data cleaning capabilities. The table of contents of this article is as follows:

图片

i. iloc

1. Definitions

The iloc indexer is used for integer position-based indexing or selection by position.

2. Grammar

[row selection, column selection]

3.Code Example

(1) Importing data

图片

(2) Select Single Row or Single Column

图片

图片

(3) Select multiple rows or columns

图片

(4) Attention

iloc select a line to return to the Series, select multiple lines to return to the DataFrame, by passing the list can be converted to DataFrame.

图片

II. Loc

1. Definitions

Loc selects data by label or index, boolean value or condition, which is a more common method of selecting data.

2. Grammar

[row selection, column selection]

3.Code Example

(1) Use loc for tag-based or index-based methods

1) First set up the index

图片

(2) According to the index to select a single row or column

图片

3) Select multiple rows and columns according to the index

图片

(2) Boolean/logical indexing using loc

图片

(3) More examples of loc selection data

图片

(4) Using loc to set values in DataFrame

图片

technical exchange

Feel free to republish, bookmark, and like something to support it!

在这里插入图片描述

The above is Python Pandas data analysis of iloc and loc usage details, more information about Python data analysis please pay attention to my other related articles!