<jupyter_start><jupyter_text>Selectors Selecting with .loc and .ilocAside from using double brackets `[][]` to access values, DataFrame provides `.loc[]` and `'iloc[]` mthods to select values with row labels (index) or position respectively.Here's some examples of using `.loc()`<jupyter_code>import pandas as pd

# read data fom csv
flights = pd.read_csv('../data/flights.csv', header=0)

# select single row by index
flights.loc[0]

# select multiple rows with slices
flights.loc[[0, 5, 7, 10]]
flights.loc[0:3]

# select multiple rows and columns by index
flights.loc[0:3,['airline', 'src', 'dest']]<jupyter_output><empty_output><jupyter_text>:::info `.loc[[rows],[columns]]`Using `.loc` the first bracket selects rows and the second bracket select column. This is the reverse order of using double brackets.:::`.iloc[]` works the same way, but instead of labels (index) you can select by row and colunm position numbers. In this case, since our flight records have a RangeIndex the row indexes are the **same** as labels:<jupyter_code># select first row
flights.iloc[0]

# select multiple rows with slices
flights.iloc[[0, 5, 7, 10]]
flights.iloc[0:3]

# select multiple rows and columns by position
flights.iloc[0:3,[0, 2, 4]]<jupyter_output><empty_output><jupyter_text>:::info Mixing `.loc` and `iloc`You can always mix using `.loc` and `iloc` together::::<jupyter_code># mixing loc and iloc
# select rows 5-10 and few columns
flights.iloc[5:10].loc[:, ['flight_number', 'src', 'dest']]<jupyter_output><empty_output><jupyter_text>Conditional SelectionsYou can specify criterias for selecting values within the Dataframe:<jupyter_code># select delta airline flights
flights.loc[flights.airline == 'DL']
# same as above
flights.loc[flights['airline'] == 'DL']

# flights where distance is not null
flights.loc[flights.distance.notna()]
# or where distance is null
flights.loc[flights.distance.isna()]

# select flights out of PDX over 500 miles
flights.loc[(flights.src == 'PDX') & (flights.distance > 500.0)]

# apply multiple conditions::
# select delta or alaska flights
flights.loc[(flights.airline == 'DL') | (flights.airline == 'AS')]
# select delta airlines flights from LAX-JFK
flights.loc[(flights.airline == 'DL') & (flights.src == 'LAX') & (flights.dest == 'JFK')]

# select delta and alaska flights from LAX-JSK
a = flights.loc[(flights.airline.isin(['DL', 'AS'])) & 
            (flights.src == 'LAX') & (flights.dest == 'JFK')]

with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.max_colwidth', 500, 'display.width', 500):
    print(a)<jupyter_output><empty_output><jupyter_text>:::tip Handy selection methodsPandas has special selections method for almsot everything. Remember them and use the rigolously. Methods such as `.isin()`, `.isna()`, and `.notna()`. See examples above.::: Using query() methodIf you are more familiar with SQL syntax, you can use the pandas `.query()` method:<jupyter_code># select flights from PDX over 500 miles
flights.query("(src == 'PDX') & (distance > 500.0)")<jupyter_output><empty_output><jupyter_text>SubselectionsYou can always save a selection and further subselect within a set by assigning your selections into a variable:<jupyter_code># select flights from PDX
pdx_flights = flights.loc[flights.src == 'PDX']
# find long distance flights
pdx_long_distance = pdx_flights.query("distance > 500.0")
pdx_long_distance<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1352137249200349, 'fraction_numerical': 0.015702239022971794, 'mean_word_length': 5.798418972332016, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '329836', 'license': 'Apache-2.0', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1121, 'n_words': 359, 'path': 'deb/ch1/pandas-intro/notebooks/03_selectors.ipynb', 'repo_name': 'turalabs/deb', 'size': '3439'}