pandas get index column name code example

Example 1: dataframe get index name

In [7]: df.index.name
Out[7]: 'Index Title'

In [8]: df.index.name = 'foo'

In [9]: df.index.name
Out[9]: 'foo'

In [10]: df
Out[10]: 
         Column 1
foo              
Apples          1
Oranges         2
Puppies         3
Ducks           4

Example 2: get column index pandas

df = pd.read_csv('thanksgiving_w_age_income.csv')

# Obtain Column Indices:
gravy = df.columns.get_loc("Do you typically have gravy?")
meet_friends = df.columns.get_loc('Have you ever tried to meet up with hometown friends on Thanksgiving night?')
friendsgiving = df.columns.get_loc('Have you ever attended a "Friendsgiving?"')