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+
731 reviews

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

Chapter 1: Matplotlib

Data visualization is a key skill for aspiring data scientists. Matplotlib makes it easy to create meaningful and insightful plots. In this chapter, you’ll learn how to build various types of plots, and customize them to be more visually appealing and interpretable.

Exercise 1: Basic plots with MatplotlibExercise 2: Line plot (1)Exercise 3: Line Plot (2): InterpretationExercise 4: Line plot (3)Exercise 5: Scatter Plot (1)Exercise 6: Scatter plot (2)Exercise 7: HistogramExercise 8: Build a histogram (1)Exercise 9: Build a histogram (2): binsExercise 10: Build a histogram (3): compareExercise 11: Choose the right plot (1)Exercise 12: Choose the right plot (2)Exercise 13: CustomizationExercise 14: LabelsExercise 15: TicksExercise 16: SizesExercise 17: ColorsExercise 18: Additional CustomizationsExercise 19: Interpretation

Chapter 2: Dictionaries & 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)

Chapter 3: Logic, Control Flow and Filtering

Chapter 4: Loops

Chapter 5: Case Study: Hacker Statistics

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