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”
Exercise instructions
- Use single square brackets to print out the
country
column ofcars
as a Pandas Series. - Use double square brackets to print out the
country
column ofcars
as a Pandas DataFrame. - Use double square brackets to print out a DataFrame with both the
country
anddrives_right
columns ofcars
, 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
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: InterpretationChapter 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
Boolean logic is the foundation of decision-making in Python programs. Learn about different comparison operators, how to combine them with Boolean operators, and how to use the Boolean outcomes in control structures. You'll also learn to filter data in pandas DataFrames using logic.
Exercise 1: Comparison OperatorsExercise 2: EqualityExercise 3: Greater and less thanExercise 4: Compare arraysExercise 5: Boolean OperatorsExercise 6: and, or, not (1)Exercise 7: and, or, not (2)Exercise 8: Boolean operators with NumPyExercise 9: if, elif, elseExercise 10: WarmupExercise 11: ifExercise 12: Add elseExercise 13: Customize further: elifExercise 14: Filtering pandas DataFramesExercise 15: Driving right (1)Exercise 16: Driving right (2)Exercise 17: Cars per capita (1)Exercise 18: Cars per capita (2)Chapter 4: Loops
There are several techniques you can use to repeatedly execute Python code. While loops are like repeated if statements, the for loop iterates over all kinds of data structures. Learn all about them in this chapter.
Exercise 1: while loopExercise 2: while: warming upExercise 3: Basic while loopExercise 4: Add conditionalsExercise 5: for loopExercise 6: Loop over a listExercise 7: Indexes and values (1)Exercise 8: Indexes and values (2)Exercise 9: Loop over list of listsExercise 10: Loop Data Structures Part 1Exercise 11: Loop over dictionaryExercise 12: Loop over NumPy arrayExercise 13: Loop Data Structures Part 2Exercise 14: Loop over DataFrame (1)Exercise 15: Loop over DataFrame (2)Exercise 16: Add column (1)Exercise 17: Add column (2)Chapter 5: Case Study: Hacker Statistics
This chapter will allow you to apply all the concepts you've learned in this course. You will use hacker statistics to calculate your chances of winning a bet. Use random number generators, loops, and Matplotlib to gain a competitive edge!
Exercise 1: Random NumbersExercise 2: Random floatExercise 3: Roll the diceExercise 4: Determine your next moveExercise 5: Random WalkExercise 6: The next stepExercise 7: How low can you go?Exercise 8: Visualize the walkExercise 9: DistributionExercise 10: Simulate multiple walksExercise 11: Visualize all walksExercise 12: Implement clumsinessExercise 13: Plot the distributionExercise 14: Calculate the oddsWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.