Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Working with Columns | 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

Working with Columns

When working with a DataFrame, you can access each column individually. Here's the syntax for doing so:

To clarify this syntax:

  • Start by writing the name of the DataFrame you're working with;
  • Next, place the column name you want to access inside square brackets. Remember to enclose the column name in quotation marks.

Let's look at an example using a DataFrame.

123456789
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) capitals = countries['capital'] print(capitals)
copy

Executing this code will display just the column containing capital cities, rather than the entire DataFrame.

You can also access multiple columns like this:

Compared to accessing a single column, there is only one difference. This time, you'll need to put the list of column names inside an additional set of square brackets — meaning you'll use double square brackets. Check out the example below.

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) columns = countries[['country', 'capital']] print(columns)
copy

Task

Retrieve the columns 'model', 'year', and 'price' (in that order) from the audi_cars DataFrame. Give it a try!

Task

Retrieve the columns 'model', 'year', and 'price' (in that order) from the audi_cars DataFrame. Give it a try!

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 1. Chapter 11
toggle bottom row

Working with Columns

When working with a DataFrame, you can access each column individually. Here's the syntax for doing so:

To clarify this syntax:

  • Start by writing the name of the DataFrame you're working with;
  • Next, place the column name you want to access inside square brackets. Remember to enclose the column name in quotation marks.

Let's look at an example using a DataFrame.

123456789
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) capitals = countries['capital'] print(capitals)
copy

Executing this code will display just the column containing capital cities, rather than the entire DataFrame.

You can also access multiple columns like this:

Compared to accessing a single column, there is only one difference. This time, you'll need to put the list of column names inside an additional set of square brackets — meaning you'll use double square brackets. Check out the example below.

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) columns = countries[['country', 'capital']] print(columns)
copy

Task

Retrieve the columns 'model', 'year', and 'price' (in that order) from the audi_cars DataFrame. Give it a try!

Task

Retrieve the columns 'model', 'year', and 'price' (in that order) from the audi_cars DataFrame. Give it a try!

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 1. Chapter 11
toggle bottom row

Working with Columns

When working with a DataFrame, you can access each column individually. Here's the syntax for doing so:

To clarify this syntax:

  • Start by writing the name of the DataFrame you're working with;
  • Next, place the column name you want to access inside square brackets. Remember to enclose the column name in quotation marks.

Let's look at an example using a DataFrame.

123456789
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) capitals = countries['capital'] print(capitals)
copy

Executing this code will display just the column containing capital cities, rather than the entire DataFrame.

You can also access multiple columns like this:

Compared to accessing a single column, there is only one difference. This time, you'll need to put the list of column names inside an additional set of square brackets — meaning you'll use double square brackets. Check out the example below.

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) columns = countries[['country', 'capital']] print(columns)
copy

Task

Retrieve the columns 'model', 'year', and 'price' (in that order) from the audi_cars DataFrame. Give it a try!

Task

Retrieve the columns 'model', 'year', and 'price' (in that order) from the audi_cars DataFrame. Give it a try!

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

When working with a DataFrame, you can access each column individually. Here's the syntax for doing so:

To clarify this syntax:

  • Start by writing the name of the DataFrame you're working with;
  • Next, place the column name you want to access inside square brackets. Remember to enclose the column name in quotation marks.

Let's look at an example using a DataFrame.

123456789
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) capitals = countries['capital'] print(capitals)
copy

Executing this code will display just the column containing capital cities, rather than the entire DataFrame.

You can also access multiple columns like this:

Compared to accessing a single column, there is only one difference. This time, you'll need to put the list of column names inside an additional set of square brackets — meaning you'll use double square brackets. Check out the example below.

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) columns = countries[['country', 'capital']] print(columns)
copy

Task

Retrieve the columns 'model', 'year', and 'price' (in that order) from the audi_cars DataFrame. Give it a try!

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 11
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt