In [ ]:
import pandas as pd

pd.set_option('display.max_columns', 500)

def a1_notation(n):
    string = ""
    while n > 0:
        n, remainder = divmod(n - 1, 26)
        string = chr(65 + remainder) + string
    return string

alist = list(range(1, 31))
A1_list = [a1_notation(x) for x in alist]
A1_list_plus = ["%s-label" % a1_notation(x) for x in alist]
df = pd.DataFrame([alist for aline in alist], columns=A1_list, index=A1_list_plus)
df

In [ ]:
# If we use just a single label-name (like using an integer in iloc) we get a Pandas Series.
df.loc['A-label']

In [ ]:
# Putting a label-name in a list returns a DataFrame.
df.loc[['A-label']]

In [ ]:
# This is how we name each row-label we want to select.
df.loc[['A-label', 'C-label', 'E-label', 'G-label', 'I-label']]

In [ ]:
# This is how we use row-labels as part of a slice defintion.
df.loc['A-label':'G-label']

In [ ]:
# As with Python slices, you can leave off the beginning slice value for "from beginning".
df.loc[:'G-label']

In [ ]:
# As with Python slices, you can leave off the ending slice value for "to the end".
df.loc['W-label':]

In [ ]:
# As with Python slices, if you leave off both beginning and end values, you select the whole thing.
df.loc[:]

In [ ]:
# As with iloc, there is a comma in the interface which seperates row arguments from column arguments.
df.loc[:, ]

In [ ]:
# If we use just a single label-name after the comma (like using an integer in iloc) we get a Pandas Series.
df.loc[:, 'A']

In [ ]:
# Putting a label-name in a list after the comma returns a DataFrame.
df.loc[:, ['A']]

In [ ]:
# This is how we perform the very common task of selecting specific columns by label.
df.loc[:, ['P', 'A', 'N', 'D', 'A', 'S']]

In [ ]:
# Label-based slice definitions can be used in place of lists.
df.loc[:, 'P':'U']

In [ ]:
# After the comma you can leave off the ending slice value for "to the end".
df.loc[:, 'P':]

In [ ]:
# After the comma you can leave off the beginning slice value for "from beginning".
df.loc[:, :'N']

In [ ]:
# This is how we get the intersection of label-based slice definitions for rows and columns.
df.loc[:'N-label', :'N']

In [ ]:
# We can use lists of labels to define intersections. If each list has one item, we get a cell value. 
df.loc[['N-label'], ['N']]

In [ ]:
# We can use references (variable names) to make list-intersection requests more readable.
rows = ['P-label', 'A-label', 'N-label', 'D-label', 'A-label', 'S-label']
cols = ['P', 'A', 'N', 'D', 'A', 'S']
df.loc[rows, cols]

In [ ]:
# We can use list comprehension and the enumerate and modulus functions to list every other column. 
[(i, x)[1] for i, x in enumerate(list(df.columns)) if i%2]

In [ ]:
# This is how we select every other column by label name.
df.loc[:, [(i, x)[1] for i, x in enumerate(list(df.columns)) if i%2]]

In [ ]: