AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Pandas Isin (): the filtering method

PARTAGEZ

The Python Pandas function DataFrame.isin() is used to check quickly and efficiently If certain values ​​are present in a dataframa. This function is particularly useful when you want to check several values ​​at the same time.

Web accommodation

Flexible, efficient and safe web accommodation

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

The syntax of the function isin() pandas

Pandas isin() takes only one parameter. Consequently, the basic syntax of the function is very simple and presents itself as follows:

DataFrame.isin(values)

python

The parameter values Maybe a Python list, a python dictionary or another data And contains the values ​​to be sought in the original dataframe.

Advice

If you do not work with the dataframas of Pandas, but rather with Pandas series, there is an equivalent function: Series.isin().

How to use the function isin() of pandas?

The possibilities of using isin() are multiple: using this function, you can not only check the presence of values, but also filter your dataaframa.

Check the presence of values ​​in a column

We will now examine a dataframe containing information on different people and their city of residence.

import pandas as pd
# Créer un exemple de DataFrame
data = {
    'Nom' : ['Alice', 'Bob', 'Charlie', 'David'],
    'Ville' : ['New York', 'Los Angeles', 'Chicago', 'Houston']
}
df = pd.DataFrame(data)
print(df)

python

Dataframa presents itself as follows:

Nom      Ville
0   Alice     New York
1     Bob  Los Angeles
2  Charlie     Chicago
3   David    Houston

We then want to check with the method isin() of pandas if certain cities are present in the corresponding column. To do this, we create a list called « cities_to_check » (translated by « cities to be checked »), then we perform the function on the DataFrame column called « City »:

# Villes à vérifier
cities_to_check = ['Chicago', 'Houston', 'Miami']
# Application de la méthode isin()
result = df['Ville'].isin(cities_to_check)
print(result)

python

The result is a series of Boolean values ​​which indicate if the city in question is present in the list of cities to be checked:

0 False
1 False
2 True
3 True
Nom : ville, dtype: bool

Filter values ​​in the dataframa with isin()

THE isin() of pandas can also be used to filter the dataframa in order to keep only the lines containing the cities present in cities_to_check.

# Filtrer le DataFrame en se basant sur isin()
filtered_df = df[df['Ville'].isin(cities_to_check)]
print(filtered_df)

python

The result is a dataframa that contains only lines with cities that are also present in the comparison list cities_to_check ::

Nom    Ville
2  Charlie   Chicago
3    David   Houston

Check several columns

To perform more complex filtering operations, isin() De Pandas can also be used with Python dictionaries. The following example shows how to work with a dictionary to simultaneously check several columns of a dataframa. To do this, the original dataframa is extended from a column and isin() is then used:

# Créer un exemple de DataFrame
data = {
    'Nom' : ['Alice', 'Bob', 'Charlie', 'David'],
    'Ville' : ['New York', 'Los Angeles', 'Chicago', 'Houston'],
    'Âge' : [25, 30, 35, 40]
}
df = pd.DataFrame(data)
# Dictionnaire des valeurs à vérifier
values_to_check = {
    'Ville' : ['Chicago', 'Houston'],
    'Âge' : [30, 40]
}
# Application de isin() avec un dictionnaire
result = df.isin(values_to_check)
print(result)

python

In this case, the appeal of function of isin() Returns a dataframe with Boolean values ​​which indicate whether the conditions are met in the respective columns:

Nom    Ville    Âge
0  False   False  False
1  False   False   True
2  False    True  False
3  False    True   True

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

Localhost: how to connect to 127.0.0.1?

When you call an IP address, you are usually trying to contact another computer on the Internet. However, if you call the IP address 127.0.0.1,

Web Marketing

What is Proxmox? – IONOS

Proxmox is an open source platform dedicated to virtualization and containerization. It allows you to manage and operate virtual machines, containers and high availability clusters.

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact