Get Started

Add column (2)

Using iterrows() to iterate over every observation of a Pandas DataFrame is easy to understand, but not very efficient. On every iteration, you're creating a new Pandas Series.

If you want to add a column to a DataFrame by calling a function on another column, the iterrows() method in combination with a for loop is not the preferred way to go. Instead, you'll want to use apply().

Compare the iterrows() version with the apply() version to get the same result in the brics DataFrame:

for lab, row in brics.iterrows() :
    brics.loc[lab, "name_length"] = len(row["country"])

brics["name_length"] = brics["country"].apply(len)

We can do a similar thing to call the upper() method on every name in the country column. However, upper() is a method, so we'll need a slightly different approach:

This is a part of the course

“Intermediate Python”

View Course

Exercise instructions

  • Replace the for loop with a one-liner that uses .apply(str.upper). The call should give the same result: a column COUNTRY should be added to cars, containing an uppercase version of the country names.
  • As usual, print out cars to see the fruits of your hard labor

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)

# Use .apply(str.upper)
for lab, row in cars.iterrows() :
    cars.loc[lab, "COUNTRY"] = row["country"].upper()
Edit and Run Code

This exercise is part of the course

Intermediate Python

BeginnerSkill Level
4.6+
776 reviews

Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

There are several techniques you can use to repeatedly execute Python code. While loops are like repeated if statements, the for loop iterates over all kinds of data structures. Learn all about them in this chapter.

Exercise 1: while loopExercise 2: while: warming upExercise 3: Basic while loopExercise 4: Add conditionalsExercise 5: for loopExercise 6: Loop over a listExercise 7: Indexes and values (1)Exercise 8: Indexes and values (2)Exercise 9: Loop over list of listsExercise 10: Loop Data Structures Part 1Exercise 11: Loop over dictionaryExercise 12: Loop over NumPy arrayExercise 13: Loop Data Structures Part 2Exercise 14: Loop over DataFrame (1)Exercise 15: Loop over DataFrame (2)Exercise 16: Add column (1)Exercise 17: Add column (2)

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free