Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Adding a New Column | The Very First Steps
Pandas First Steps
course content

Course Content

Pandas First Steps

Pandas First Steps

1. The Very First Steps
2. Reading Files in Pandas
3. Analyzing the Data

bookAdding a New Column

We've learned how to create a DataFrame. Now let's explore what we can do with it.

First, let's craft a compact DataFrame consisting of three columns and seven rows.

1234567
import pandas as pd dataset = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(dataset) print(countries)
copy

You can expand the DataFrame by adding new columns, and there are multiple ways to do it. We'll focus on two methods. The syntax for the first method is as follows:

  • dataframe is the name of our existing DataFrame to which we'll add new columns;

  • name_of_new_column is the name you're giving to the new column you're adding.;

  • value_1, value_2, value_3 are the values that will populate the new column.

Note

The name of the new column should be enclosed in quotation marks and wrapped in square brackets. Likewise, the values inserted into the new column should also be within square brackets. If the values are numeric, they don't need to be in quotes; if they're strings, then quotes are necessary.

Now, let's demonstrate how to add a population column to our pre-existing countries DataFrame.

12345678
import pandas as pd dataset = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(dataset) countries['population'] = [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200] print(countries)
copy

Note

Using this method, the new column will be appended to the end of the DataFrame.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7
We're sorry to hear that something went wrong. What happened?
some-alt