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:
import pandas as pd
import numpy as np
from collections import namedtuple
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
# Creation of a namedtuple called "Student" with "index" fields and df DataFrame columns
Student = namedtuple("Student", ["index"] + df.columns.tolist())
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()