breeds = [“Labrador”, “Poodle”, “Chow Chow”, “Schnauzer”, “Chihuahua”, “St. Bernard”]
breeds[2:5] # 2 refers to the third elements and the 5 is not included in the slice
breeds[:3] # To slice from the beginning of the list, can omit the zero
breeds[ : ] # Slicing with colon on its own returns the whole list
dogs_srt = dogs.set_index([“breed”, “color”]).sort_index()
print(dog_srt)
dogs_str.loc[“Chow Chow” : “Poodle”]
dogs.str.loc[(“Labrador”, “Brown”) : (“Schnauzer”, “Grey”)]
temperatures_srt = temperatures_ind.sort_index() # Sort the index of temperatures_ind
print(temperatures_srt.loc["Pakistan" : "Russia"]) # Subset rows from Pakistan to Russia
print(temperatures_srt.loc["Lahore" : "Moscow"]) # Subset rows from Lahore to Moscow
print(temperatures_srt.loc[("Pakistan", "Lahore") : ("Russia", "Moscow")]) # Subset rows from Pakistan, Lahore to Russia, Moscow
dogs.str.loc[:, “name” : “height_cm”]
dogs.str.loc[(“Labrador”, “Brown”) : (“Schnauzer”, “Grey”), “name” : “height_cm”]
print(temperatures_srt.loc[("India", "Hyderabad") : ("Iraq", "Baghdad")]) # Subset rows from India, Hyderabad to Iraq, Baghdad
print(temperatures_srt.loc[:,"date" : "avg_temp_c"]) # Subset columns from date to avg_temp_c
print(temperatures_srt.loc[("India", "Hyderabad") : ("Iraq", "Baghdad"), "date":"avg_temp_c"]) # Subset in both directions at once
dogs = dog.set_index(“date_of_birth”).sort_index()
dogs.loc[“2014-08-25” : “2016-09-16”]
print(temperatures[(temperatures["date"] >= "2010") & (temperatures["date"] < "2012")]) # Use Boolean conditions to subset temperatures for rows in 2010 and 2011
temperatures_ind = temperatures.set_index("date") # Set date as an index
print(temperatures_ind.loc["2010":"2011"]) # Use .loc[] to subset temperatures_ind for rows in 2010 and 2011
print(temperatures_ind.loc["2010-08":"2011-02"]) # Use .loc[] to subset temperatures_ind for rows from Aug 2010 to Feb 2011