Skip to content

Latest commit

 

History

History
79 lines (56 loc) · 2.76 KB

Pandas_Iterate_over_DataFrame_rows_as_namedtuples.md

File metadata and controls

79 lines (56 loc) · 2.76 KB



Template request | Bug report | Generate Data Product

Tags: #pandas #python #loops #snippet #operations #namedtuples #dataframe

Author: Benjamin Filly

Description: This notebook demonstrates how to iterate over DataFrame rows as namedtuples

References:

Input

Import libraries

import pandas as pd
import numpy as np
from collections import namedtuple

Model

Create Sample Dataframe

dict1 = {
    "student_id": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], # List of student IDs
    "student_name": [
        "Peter",
        "Dolly",
        "Maggie",
        "David",
        "Isabelle",
        "Harry",
        "Akin",
        "Abbey",
        "Victoria",
        "Sam",
    ], # List of student names
    "student_course": np.random.choice(["Biology", "Physics", "Chemistry"], size=10), # List of random courses for each student
}

# DataFrame creation from the dictionary
df = pd.DataFrame(dict1)
df

Definition of namedtuple

# Creation of a namedtuple called "Student" with "index" fields and df DataFrame columns
Student = namedtuple("Student", ["index"] + df.columns.tolist())

Output

Display result

for row in df.itertuples(index=True, name="Student"):# Iteration on df DataFrame rows using itertuples
    student = Student(*row) # Create a Student object from each line
    print("Index:", student.index) # Display student information
    print("Student ID:", student.student_id)
    print("Name:", student.student_name)
    print("Course:", student.student_course)
    print()