Get startedGet started for free

Loop over DataFrame (2)

The row data that's generated by iterrows() on every run is a Pandas Series. This format is not very convenient to print out. Luckily, you can easily select variables from the Pandas Series using square brackets:

for lab, row in brics.iterrows() :
    print(row['country'])

This exercise is part of the course

Intermediate Python for Data Science

View Course

Exercise instructions

Adapt the code in the for loop such that the first iteration prints out "US: 809", the second iteration "AUS: 731", and so on. Make sure to print out this exact string, with the correct spacing.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Adapt for loop
for lab, row in cars.iterrows() :
    print(lab)
    print(row)
Edit and Run Code