AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Pandas loc[] : explanation of the function

PARTAGEZ

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 columns of a dataframe can be extracted in a targeted way.

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 loc[]

Go to parameter to loc[] there Selection of desired labels. For the rest, the syntax is very simple:

DataFrame.loc[selection]

python

With pandas loc[]selection is mainly based on labels. The past parameter can therefore be a single label, a list or a beach of labels. It is also possible to use Boolean tables.

loc[] vs. iloc[]

While Pandas DataFrame.loc[] Select data based on labels, DataFrame.iloc, whose consonance is similar, is used to select data based on whole positions.

The following example makes it possible to illustrate the differences between these two parameters. First of all, we create a dataframe pandas:

import pandas as pd
# Exemple de DataFrame
data = {'Nom': ['Anna', 'Bob', 'Chris'], 'Âge': [23, 35, 30]}
df = pd.DataFrame(data)
print(df)

python

The resulting dataframa looks like the following:

Nom  Âge
0  Anna    23
1    Bob    35
2 Chris    30

To extract « Anna » from DataFrame, Pandas loc[] And iloc[] can be used. The two methods give the same result, but loc[] uses an index based on labels while iloc[] uses a digital index.

# Utilisation de loc pour trouver l’étiquette
print(df.loc[0, 'Nom'])  # Sortie : 'Anna'
# Utilisation de iloc pour trouver la position
print(df.iloc[0, 0])  # Sortie : 'Anna'

python

Pandas application DataFrame.loc[]

Pandas loc[] help you Extract sub-assemblies from your dataframe. It may be one or more lines or columns: indeed, loc[] can be used in different cases.

Single line selection

We will now examine an example of data with the following data:

import pandas as pd
data = {
    'Nom': ['Anna', 'Bob', 'Chris'],
    'Âge': [23, 35, 30],
    'Ville': ['Paris', 'Lyon', 'Marseille']
}
df = pd.DataFrame(data)
print(df)

python

The resulting dataframa is as follows:

Nom  Âge     Ville
0   Anna   23     Paris
1    Bob   35      Lyon
2  Chris   30  Marseille

To select line data with index 1 (corresponding to Bob), Pandas are used loc[] ::

bob_data = df.loc[1]
print(bob_data)

python

The result is in line with expectations:

Nom       Bob
Âge        35
Ville    Lyon
Name: 1, dtype: object

Selection of several columns

Pandas DataFrame.loc[] is useful for selecting a subset of columns. Using :we select all the lines. The following code selects the columns « Name » and « City » for all lines:

nom_ville = df.loc[:, ['Nom', 'Ville']]
print(nom_ville)

python

The result is a subset of the original dataframa:

Nom      Ville
0   Anna     Paris
1    Bob      Lyon
2  Chris  Marseille

Conditional selection

With pandas loc[]it is also possible to filter the lines according to a condition. To do this, simply use Boolean comparison operators. For example, in the following code, all people over the age of 25 must be filtered:

older_than_25 = df.loc[df['Âge'] > 25]
print(older_than_25)

python

The above code returns the following dataframa, which only contains data from people over 25 years old:

Nom  Âge        Ville
1    Bob    35        Lyon
2 Chris    30  Marseille

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