Add column (1)
In the video, Hugo showed you how to add the length of the country names of the brics DataFrame in a new column:
for lab, row in brics.iterrows() :
brics.loc[lab, "name_length"] = len(row["country"])
You can do similar things on the cars DataFrame.
This exercise is part of the course
Intermediate Python
Exercise instructions
- Use a
forloop to add a new column, namedCOUNTRY, that contains a uppercase version of the country names in the"country"column. You can use the string methodupper()for this. - To see if your code worked, print out
cars. Don't indent this code, so that it's not part of theforloop.
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)
# Code for loop that adds COUNTRY column
# Print cars
print(cars)