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