Python Pandas program to iterate over rows in a DataFrame

In this python pandas program, we will iterate over rows in a DataFrame using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Iterate over rows in a DataFrame using for loop and df.iterrows().
  4. Print the records for each column using row[column1],row[columns2],……,row[column_n].
				
					import pandas as pd
import numpy as np
d = [{'name':'Yash','percentage':78},{'name':'Rakesh','percentage':80},{'name':'Suresh','percentage':60}]
df = pd.DataFrame(d)
for index, row in df.iterrows():
    print(row['name'], row['percentage'])
				
			

Output :

				
					Yash 78
Rakesh 80
Suresh 60

				
			

add a new column in a DataFrame

get a list of column headers from the DataFrame

Leave a Comment