<jupyter_start><jupyter_text>Pandas Indexing CheatsheetThis notebook has the code that corresponds to the items in the Pandas Indexing Cheatsheet pdf.<jupyter_code>import pandas as pd
df = pd.DataFrame({'company': ['Widgets, Inc.', 'ABC, LLC', 'ACME'],
                   'city': ['Jay City', 'Wanton', 'Bland'],
                   'state': ['MO', 'TX', 'AL'],
                   'zip': ['46239', '94562', '32329']},
                  index=list("abc"))
s = df['city']<jupyter_output><empty_output><jupyter_text>Array Indexing Operator (```[]```)<jupyter_code># DataFrame
df['company'] # Series, entire column
df[['company']] # DataFrame, with one column
df[['company', 'city']] # DataFrame, multiple columns
# Series
s[0] # scalar, selecting by location
s['a'] # scalar, selecting by index label<jupyter_output><empty_output><jupyter_text>```loc``` Indexer, selecting by index label<jupyter_code># DataFrame
df.loc['a'] # Series, first row matching index label
df.loc[['a', 'c']] # DataFrame, selecting by multiple labels
df.loc['a', 'city'] # scalar, selecting by row and column label
df.loc[['a', 'c'], 'city'] # Series, selecting by labels and columns
df.loc[['a', 'c'], ['city', 'zip']] # DataFrame, selecting by labels and columns
# Series
s.loc['a'] # scalar, selecting by index label
s.loc[['a', 'c']] # Series, selecting by multiple labels
s.loc[['a']] # Series, selecting by a single label<jupyter_output><empty_output><jupyter_text>iloc Indexer - selecting by row and column offset<jupyter_code># DataFrame
df.iloc[0] # Series, first row by row location
df.iloc[[0,2]] # DataFrame, selecting by multiple rows by location
df.iloc[0,1] # scalar, selecting by row and column offset
df.iloc[[0,2], 1] # Series, selecting by rows and a single column
df.iloc[[0,2], [1,3]] # DataFrame, selecting by rows and columns
# Series
s.iloc[0] # scalar, selecting by offset
s.iloc[-1] # scalar, selecting by relative offset
s.iloc[[0,2]] # Series, selecting by multiple offsets
s.iloc[[0]] # Series, selecting by a single offset<jupyter_output><empty_output><jupyter_text>Slicing<jupyter_code># DataFrame
df[0:2] # DataFrame, slice by row location (doesn't include end)
df.loc['a':'c'] # DataFrame, slice by label (note rows selected, inclusive of end)
df.iloc[0:2] # DataFrame, slice by row location (doesn't include end)
df.loc['a':'c', 'city':'zip'] # DataFrame, slice both rows and columns (inclusive for both)
df.iloc[0:3, 1:4] # DataFrame, slice by both rows and columns (doesn't include end)
#Series
s[:]  # full slice
print(s[:2]) # all ways to slice first two elements in Series
print(s[0:2])
print(s[0:2:1])
print(s[slice(0,2,1)])
s['b':'c'] # slice by label, inclusive
s.loc['b':'d'] # slice by label, inclusive
s.iloc[1:3] # slice by location, not inclusive<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16100178890876565, 'fraction_numerical': 0.018604651162790697, 'mean_word_length': 5.28314606741573, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '125606', 'license': 'CC0-1.0', 'n_tokens_mistral': 969, 'n_tokens_neox': 935, 'n_words': 303, 'path': 'pandas/indexing_cheatsheet.ipynb', 'repo_name': 'wrighter/python_blogposts', 'size': '2795'}