AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Presentation of Pandas Iloc[] – ionos

PARTAGEZ

The Python Pandas DataFrame property iloc[] is used to select data from a pandas dataframe Depending on their index. Thus, specific lines and columns of a dataframa can be visualized according to their position.

Web accommodation

Flexible, efficient and safe web accommodation

  • SSL certificate and DDOS protection
  • Data backup and restoration
  • Assistance 24/7 and personal advisor

Pandas syntax iloc[]

Pandas iloc[] Accepts integers who specify The elements to select in the DataFrame. General syntax for pandas DataFrame.iloc() East :

DataFrame.iloc[selection]

python

Pandas iloc[] Accepts several types of arguments: A single integer, a python list of whole, a slice object or a tuple with indices of lines and columns.

Use of pandas DataFrame.iloc[]

Pandas property behavior iloc[] varies depending on the value you give it. Here are some practical examples to illustrate this point.

Selection of a specific line

We start by creating a dataframa with different people, their age as well as their place of residence:

import pandas as pd
# Création d'un DataFrame d'exemple
données = {'Nom': ['Anna', 'Bob', 'Charlie', 'David'],
    'Âge': [28, 24, 22, 32],
    'Ville': ['Paris', 'Marseille', 'Lyon', 'Toulouse']}
df = pd.DataFrame(données)
print(df)

python

The resulting dataframa looks like the following:

Nom  Âge      Ville
0     Anna   28      Paris
1      Bob   24  Marseille
2  Charlie   22    Lyon
3    David   32   Toulouse

Using iloc[]you can now select any line by passing the desired line index:

# Sélection de la première ligne
résultat = df.iloc[0]
print(résultat)

python

In this example, the zero line (0) is selected. The result is data for Anna:

Nom       Anna
Âge         28
Ville    Paris
Name: 0, dtype: object

Selection of a specific line and column

If you want to specify not only the index of the line but also that of the column, simply pass these values ​​to iloc[] by separating them with a comma:

# Sélection de la première ligne et de la première colonne
résultat = df.iloc[0, 1]
print(résultat)

python

The call to Pandas iloc[] Above allows you to select the zero line (0) and the first column (1). The result is therefore the age of Anna: 28.

Selection of several lines and columns with intervals (:))

You can also select several lines and columns at the same time using Python slices. In a slice (start:stop), the value stop is excluded from the selection.

# Sélection des deux premières lignes et des deux premières colonnes
résultat = df.iloc[0:2, 0:2]
print(résultat)

python

The code output above is as follows:

Nom  Âge
0  Anna   28
1   Bob   24

Here, the first two lines (0:2) and the first two columns (0:2) are selected. The resulting dataframa contains only the corresponding extracts.

Selection of specific lines and columns with lists

Several columns and lines can be selected using Python lists. The advantage of these lists is that they also make it possible to select non -consecutive lines and columns in the DataFrame:

# Sélection de la première et de la troisième ligne ainsi que de la deuxième et troisième colonne
résultat = df.iloc[[0, 2], [1, 2]]
print(résultat)

python

Here, the zero line and the second line ([0, 2]) as well as the first and second column ([1, 2]) are selected, which gives the following output:

Âge   Ville
0   28  Paris
2   22   Lyon

In Python, iloc[] is an essential tool for accessing data according to their position. It is often used in addition to loc[]which selects data by label.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Web Marketing

Ubuntu FTP server: How to configure it?

An Ubuntu FTP server allows both downloading and sending files, each access being controlled by a separate connection. In this tutorial on the Ubuntu FTP

Web Marketing

Pandas loc[] : explanation of the function

Pandas DataFrame.loc[] is a DataFrame property in the Python Pandas library used to select data from a dataaframa depending on labels. Thus, the lines and

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact