Working with Columns
When working with a DataFrame, you can access each column individually.
df['column_name']
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:
- Is a valid Python identifier (e.g., no spaces, special characters, or starting with a number);
- Does not conflict with an existing
pandas
attribute or method name.
df.column_name
12345678910111213import 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)
Executing this code will display just the column containing capital cities, rather than the entire DataFrame.
You can also access multiple columns like this:
df[['column1', 'column2', 'column3']]
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.
12345678import 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)
Swipe to start coding
You are given a DataFrame
named audi_cars
.
- Retrieve the data for the columns
'model'
,'year'
, and'price'
and store the result in thecolumns
variable.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.03Awesome!
Completion rate improved to 3.03
Working with Columns
When working with a DataFrame, you can access each column individually.
df['column_name']
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:
- Is a valid Python identifier (e.g., no spaces, special characters, or starting with a number);
- Does not conflict with an existing
pandas
attribute or method name.
df.column_name
12345678910111213import 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)
Executing this code will display just the column containing capital cities, rather than the entire DataFrame.
You can also access multiple columns like this:
df[['column1', 'column2', 'column3']]
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.
12345678import 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)
Swipe to start coding
You are given a DataFrame
named audi_cars
.
- Retrieve the data for the columns
'model'
,'year'
, and'price'
and store the result in thecolumns
variable.
Solution
Thanks for your feedback!
single
Awesome!
Completion rate improved to 3.03
Working with Columns
Swipe to show menu
When working with a DataFrame, you can access each column individually.
df['column_name']
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:
- Is a valid Python identifier (e.g., no spaces, special characters, or starting with a number);
- Does not conflict with an existing
pandas
attribute or method name.
df.column_name
12345678910111213import 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)
Executing this code will display just the column containing capital cities, rather than the entire DataFrame.
You can also access multiple columns like this:
df[['column1', 'column2', 'column3']]
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.
12345678import 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)
Swipe to start coding
You are given a DataFrame
named audi_cars
.
- Retrieve the data for the columns
'model'
,'year'
, and'price'
and store the result in thecolumns
variable.
Solution
Thanks for your feedback!