Pandas DataFrame index and columns attributes

Filed Under: Pandas
Pandas Dataframe Index Columns

Pandas DataFrame index and columns attributes allow us to get the rows and columns label values.

We can pass the integer-based value, slices, or boolean arguments to get the label information.

Pandas DataFrame index

Let’s look into some examples of getting the labels of different rows in a DataFrame object. Before we look into the index attribute usage, we will create a sample DataFrame object.

import pandas as pd

d1 = {'Name': ['John', 'Jane', 'Mary'], 'ID': [1, 2, 3], 'Role': ['CEO', 'CTO', 'CFO']}

df = pd.DataFrame(d1, index=['A', 'B', 'C'])

print('DataFrame:\n', df)

Output:

DataFrame:
    Name  ID Role
A  John   1  CEO
B  Jane   2  CTO
C  Mary   3  CFO

1. Getting Label Name of a Single Row

row_1_label = df.index[1]
print(type(row_1_label))  # <class 'str'>
print(row_1_label)  # B

2. Getting Labels of Multiple Rows

rows_labels = df.index[[1, 2]]
print(type(rows_labels))  # <class 'pandas.core.indexes.base.Index'>
print(rows_labels)  # Index(['B', 'C'], dtype='object')

3. Slicing with DataFrame index

rows_labels = df.index[1:3]
print(type(rows_labels))  # <class 'pandas.core.indexes.base.Index'>
print(rows_labels)  # Index(['B', 'C'], dtype='object')

4. Boolean with DataFrame index

rows_labels = df.index[[True, False, True]]
print(type(rows_labels))  # <class 'pandas.core.indexes.base.Index'>
print(rows_labels)  # Index(['A', 'C'], dtype='object')

We can’t set the rows label value using the DataFrame index attribute. If we try to do that, it will raise TypeError(“Index does not support mutable operations”).

df.index[0] = 'a'  # TypeError: Index does not support mutable operations

Python DataFrame columns

The DataFrame columns attribute provides the label values for columns. It’s very similar to the index attribute. We can’t set the columns label value using this attribute. Let’s look at some examples of using the DataFrame columns attribute. We will reuse the earlier defined DataFrame object for these examples.

1. Getting a Single Column Label

column_1_label = df.columns[1]
print(type(column_1_label))  # <class 'str'>
print(column_1_label)  # ID

2. Getting Labels of Multiple Columns

columns_labels = df.columns[[1, 2]]
print(type(columns_labels))  # <class 'pandas.core.indexes.base.Index'>
print(columns_labels)  # Index(['ID', 'Role'], dtype='object')

3. Slicing with DataFrame columns

columns_labels = df.columns[1:3]
print(columns_labels)  # Index(['ID', 'Role'], dtype='object')

4. Boolean with DataFrame columns

columns_labels = df.columns[[False, False, True]]
print(columns_labels)  # Index(['Role'], dtype='object')

Conclusion

Pandas DataFrame index and columns attributes are helpful when we want to process only specific rows or columns. It’s also useful to get the label information and print it for future debugging purposes.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages