Slicing in both directions
You've seen slicing DataFrames by rows and by columns, but since DataFrames are two-dimensional objects, it is often natural to slice both dimensions at once. That is, by passing two arguments to .loc[]
, you can subset by rows and columns in one go.
pandas
is loaded as pd
. temperatures_srt
is indexed by country and city, has a sorted index, and is available.
This exercise is part of the course
Data Manipulation with pandas
Exercise instructions
- Use
.loc[]
slicing to subset rows from India, Hyderabad to Iraq, Baghdad. - Use
.loc[]
slicing to subset columns fromdate
toavg_temp_c
. - Slice in both directions at once from Hyderabad to Baghdad, and
date
toavg_temp_c
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Subset rows from India, Hyderabad to Iraq, Baghdad
print(____)
# Subset columns from date to avg_temp_c
print(____)
# Subset in both directions at once
print(____)