AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Pandas Itterows () method ()

PARTAGEZ

The Python Pandas function DataFrame.iterrows() is used to iterate on lines of a dataframe pandas. For each line, it returns a tuple python containing the index as well as a series representing the data of the corresponding line.

Web accommodation

Flexible, efficient and safe web accommodation

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

How is the syntax of the method iterrows() of pandas?

Pandas base syntax DataFrame.iterrows() is simple, because the function does not take No parameter ::

In the example of code above, df designates the dataframe on which iteration must be carried out.

How to apply the Pandas function iterrows() ?

The function DataFrame.iterrows() is typically used each time we owe Processing line data by line. It is therefore often combined with the loop for in python.

The sum of the values ​​of a column

In what follows, we consider a dataframe with the columns « name », « age » and « points »:

import pandas as pd
# Créer un exemple de DataFrame
data = {'Nom' : ['Anna', 'Ben', 'Clara'],
    'Âge' : [23, 35, 29],
    'Points' : [88, 92, 85]}
df = pd.DataFrame(data)
print(df)

python

The above code results in the following dataframe:

Nom  Âge  Points
0   Anna   23     88
1    Ben   35     92
2  Clara   29     85

We must now calculate the sum of the points. For this, we can use DataFrame.iterrows() of pandas:

# Calculer la somme des points
total_score = 0
for index, row in df.iterrows():
    total_score += row['Points']
print(f"La somme totale des points est : {total_score}")

python

In this example, the Pandas function iterrows() is used to iterate on the lines and successively add the values ​​of the « Points » column. The result is thus:

La somme totale des points est : 265

Note

When you work with iterrows() of pandas, it is important to Never modify the data directly on which you itere. Depending on the type of data, this can lead to unexpected results and cause unpredictable behavior.

Conditional edition of lines

The function iterrows() Can also be used to apply conditions to certain lines of your dataaframa. For example, suppose you want to extract the names of all people over 30 years old in an existing dataframa:

# Récupérer le nom des personnes de plus de 30 ans
names = []
for index, row in df.iterrows():
    if row['Âge'] > 30:
        names.append(row['Nom'])
print(f"Personnes de plus de 30 ans : {names}")

python

In the example of code, the dataframa lines are covered using the function DataFrame.iterrows() of pandas. In the loop forthe values ​​of the “age” column are examined in order to store only the names of people over 30 years old in the Python list names. For this, we use the Python function append(). This operation gives the following result:

Personnes de plus de 30 ans : ['Ben']

Note

Although DataFrame.iterrows() of pandas is easy to use, it should be used with caution due to its potentially reduced efficiency for bulky dataframas. In many cases, there are more efficient alternatives, such as apply() or vector calculation with pandas.

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