"""
# Chapter 4: Selecting Subsets of Data
## Recipes
* [Selecting Series data](#Selecting-Series-data)
* [Selecting DataFrame rows](#Selecting-DataFrame-rows)
* [Selecting DataFrame rows and columns simultaneously](#Selecting-DataFrame-rows-and-columns-simultaneously)
* [Selecting data with both integers and labels](#Selecting-data-with-both-integers-and-labels)
* [Speeding up scalar selection](#Speeding-up-scalar-selection)
* [Slicing rows lazily](#Slicing-rows-lazily)
* [Slicing lexicographically](#Slicing-Lexicographically)
"""
import pandas as pd
import numpy as np
"""
# Selecting Series data
"""
college = pd.read_csv('../input/pandas-cookbook-data/data/college.csv', index_col='INSTNM')
city = college['CITY']
city.to_frame().head()
city.iloc[3]
city.iloc[[10,20,30]]

city.iloc[4:50:10]
city.loc['Heritage Christian University']
np.random.seed(1)
labels = list(np.random.choice(city.index, 4))
labels
city.loc[labels]
city.loc['Alabama State University':'Reid State Technical College':10]
city['Alabama State University':'Reid State Technical College':10]
"""
## There's more...
"""
city.iloc[[3]]
city.loc['Reid State Technical College':'Alabama State University':10]
city.loc['Reid State Technical College':'Alabama State University':-10]
"""
# Selecting DataFrame rows
"""
college = pd.read_csv('../input/pandas-cookbook-data/data/college.csv', index_col='INSTNM')
college.head()
pd.options.display.max_rows = 6
college.iloc[60].to_frame().T
college.loc['University of Alaska Anchorage']
college.iloc[[60, 99, 3]]
labels = ['University of Alaska Anchorage',
          'International Academy of Hair Design',
          'University of Alabama in Huntsville']
college.loc[labels]
college.iloc[99:102]
start = 'International Academy of Hair Design'
stop = 'Mesa Community College'
college.loc[start:stop]
"""
# There's more...
"""
college.iloc[[60, 99, 3]].index.tolist()
"""
# Selecting DataFrame rows and columns simultaneously
"""
college = pd.read_csv('../input/pandas-cookbook-data/data/college.csv', index_col='INSTNM')
college.iloc[:3, :4]
college.loc[:'Amridge University', :'MENONLY']
college.iloc[:, [4,6]].head()
college.loc[:, ['WOMENONLY', 'SATVRMID']]
college.iloc[[100, 200], [7, 15]]
rows = ['GateWay Community College', 'American Baptist Seminary of the West']
columns = ['SATMTMID', 'UGDS_NHPI']
college.loc[rows, columns]
college.iloc[5, -4]
college.loc['The University of Alabama', 'PCTFLOAN']
college.iloc[90:80:-2, 5]
start = 'Empire Beauty School-Flagstaff'
stop = 'Arizona State University-Tempe'
college.loc[start:stop:-2, 'RELAFFIL']
"""
# Selecting data with both integers and labels
"""
college = pd.read_csv('../input/pandas-cookbook-data/data/college.csv', index_col='INSTNM')
col_start = college.columns.get_loc('UGDS_WHITE')
col_end = college.columns.get_loc('UGDS_UNKN') + 1
col_start, col_end
college.iloc[:5, col_start:col_end]
"""
# There's more...
"""
row_start = college.index[10]
row_end = college.index[15]
college.loc[row_start:row_end, 'UGDS_WHITE':'UGDS_UNKN']
"""
# Speeding up scalar selection
"""
college = pd.read_csv('../input/pandas-cookbook-data/data/college.csv', index_col='INSTNM')
cn = 'Texas A & M University-College Station'
college.loc[cn, 'UGDS_WHITE']
college.at[cn, 'UGDS_WHITE']
%timeit college.loc[cn, 'UGDS_WHITE']
%timeit college.at[cn, 'UGDS_WHITE']
row_num = college.index.get_loc(cn)
col_num = college.columns.get_loc('UGDS_WHITE')
row_num, col_num
%timeit college.iloc[row_num, col_num]
%timeit college.iat[row_num, col_num]
%timeit college.iloc[5, col_num]
%timeit college.iat[5, col_num]
"""
## There's more...
"""
state = college['STABBR']
state.iat[1000]
state.at['Stanford University']
"""
# Slicing rows lazily
"""

college[10:20:2]
city[10:20:2]
college.index[4001]
start = 'Mesa Community College'
stop = 'Spokane Community College'
college[start:stop:1500]
city[start:stop:1500]
"""
## There's more...
"""
college[:10, ['CITY', 'STABBR']]
first_ten_instnm = college.index[:10]
college.loc[first_ten_instnm, ['CITY', 'STABBR']]
"""
# Slicing Lexicographically
"""

college.loc['Sp':'Su']
college = college.sort_index()
college.head()
pd.options.display.max_rows = 6
college.loc['Sp':'Su']
college = college.sort_index(ascending=False)
college.index.is_monotonic_decreasing
college.loc['E':'B']
college.loc['E':'B']