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

Conteúdo do Curso

Pandas First Steps

Pandas First Steps

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

book
Working with Columns

When working with a DataFrame, you can access each column individually.

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.

Alternatively, you can use dot notation to access a column if the column name:

  1. Is a valid Python identifier (e.g., no spaces, special characters, or starting with a number);
  2. Does not conflict with an existing pandas attribute or method name.
12345678910111213
import pandas as pd countries_data = {'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(countries_data) capitals = countries['capital'] # Second option # 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.

12345678
import pandas as pd countries_data = {'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(countries_data) columns = countries[['country', 'capital']] print(columns)
copy
Tarefa
test

Swipe to show code editor

Retrieve the columns 'model', 'year', and 'price' (in that order) from the audi_cars DataFrame.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 11
toggle bottom row

book
Working with Columns

When working with a DataFrame, you can access each column individually.

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.

Alternatively, you can use dot notation to access a column if the column name:

  1. Is a valid Python identifier (e.g., no spaces, special characters, or starting with a number);
  2. Does not conflict with an existing pandas attribute or method name.
12345678910111213
import pandas as pd countries_data = {'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(countries_data) capitals = countries['capital'] # Second option # 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.

12345678
import pandas as pd countries_data = {'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(countries_data) columns = countries[['country', 'capital']] print(columns)
copy
Tarefa
test

Swipe to show code editor

Retrieve the columns 'model', 'year', and 'price' (in that order) from the audi_cars DataFrame.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 11
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt