Get Started

Square Brackets (1)

In the video, you saw that you can index and select Pandas DataFrames in many different ways. The simplest, but not the most powerful way, is to use square brackets.

In the sample code, the same cars data is imported from a CSV files as a Pandas DataFrame. To select only the cars_per_cap column from cars, you can use:

cars['cars_per_cap']
cars[['cars_per_cap']]

The single bracket version gives a Pandas Series, the double bracket version gives a Pandas DataFrame.

This is a part of the course

“Intermediate Python”

View Course

Exercise instructions

  • Use single square brackets to print out the country column of cars as a Pandas Series.
  • Use double square brackets to print out the country column of cars as a Pandas DataFrame.
  • Use double square brackets to print out a DataFrame with both the country and drives_right columns of cars, in this order.

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)

# Print out country column as Pandas Series


# Print out country column as Pandas DataFrame


# Print out DataFrame with country and drives_right columns

This exercise is part of the course

Intermediate Python

BeginnerSkill Level
4.6+
772 reviews

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

Learn about the dictionary, an alternative to the Python list, and the pandas DataFrame, the de facto standard to work with tabular data in Python. You will get hands-on practice with creating and manipulating datasets, and you’ll learn how to access the information you need from these data structures.

Exercise 1: Dictionaries, Part 1Exercise 2: Motivation for dictionariesExercise 3: Create dictionaryExercise 4: Access dictionaryExercise 5: Dictionaries, Part 2Exercise 6: Dictionary Manipulation (1)Exercise 7: Dictionary Manipulation (2)Exercise 8: DictionariceptionExercise 9: Pandas, Part 1Exercise 10: Dictionary to DataFrame (1)Exercise 11: Dictionary to DataFrame (2)Exercise 12: CSV to DataFrame (1)Exercise 13: CSV to DataFrame (2)Exercise 14: Pandas, Part 2Exercise 15: Square Brackets (1)
Exercise 16: Square Brackets (2)Exercise 17: loc and iloc (1)Exercise 18: loc and iloc (2)Exercise 19: loc and iloc (3)

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