AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

How does the Python Pandas Any () function work?

PARTAGEZ

The function of the Python Pandas library DataFrame.any() is used to determine whether at least one value in a dataframa pandas is assessed to True (True) along a given axis.

Pandas syntax any()

The basic syntax of the Pandas function any() is not complicated and presents itself as follows:

DataFrame.any(axis=0, bool_only=None, skipna=True)

python

Note

The function numpy.any() of the NUMPY library operates in a similar way to DataFrame.any () in pandas, but applies to NUMPY paintings.

Relevant parameters

The function accepts different parameters:

Parameters Description Default value
axis Determine whether the method is applied along the lines (0 or index) or columns (1 or columns) 0
skipna Indicates whether nan values ​​should be ignored True
bool_only If Trueonly columns containing Boolean values ​​(True/False) are taken into account. False

Pandas application DataFrame.any()

Example 1: Check if there are values True in any line

One of the most common use cases of the function any() is to check if at least one value True is present in a column. This can be useful when conditions should be checked.

import pandas as pd
# Créer un dataframe avec trois colonnes et trois lignes
data = {
    ‘A’: [0, 0, 0],
    ‘B’: [True, False, False],
    ‘C’: [False, False, False]
}
df = pd.DataFrame(data)
# Application de la fonction any() pour vérifier si au moins une valeur True est présente dans chaque colonne
result = df.any(axis=0)
print(result)

python

In the above code example, the Pandas function DataFrame.any() refers a series which indicates that only column B contains at least one value evaluated to True. The exit presents itself as follows:

A    False
B    True
C    False
dtype: bool

Example 2: Check if there are values True in any column

In the same way as for the first example, we can also check if there is at least one value True in any column by passing axis=1 in parameter:

result = df.any(axis=1)
print(result)

python

The output indicates that there is a value True that in the first line (index 0):

0     True
1    False
2    False
dtype: bool

Note

Reminder: in python and computer science in general, indexing begins at 0.

Web accommodation

Flexible, efficient and safe web accommodation

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

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

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact