Like df.columns for column labels, df.index exposes the row-axis Index — by default a RangeIndex of sequential integers starting at 0, but often replaced with something more meaningful, like a column of dates or IDs, via set_index(). Reading df.index shows the current row labels and their dtype; assigning a new sequence of matching length directly to df.index replaces every row label at once, the row-axis equivalent of reassigning df.columns.
1Understanding df.index
Like df.columns for column labels, df.index exposes the row-axis Index — by default a RangeIndex of sequential integers starting at 0, but often replaced with something more meaningful, like a column of dates or IDs, via set_index(). Reading df.index shows the current row labels and their dtype; assigning a new sequence of matching length directly to df.index replaces every row label at once, the row-axis equivalent of reassigning df.columns.
Use df.set_index('column_name') to promote an existing column into the DataFrame's index, which drops it as a regular column by default, rather than manually reading that column's values and assigning them to df.index yourself.
import pandas as pd
df = pd.DataFrame({"name": ["Alice", "Bob"]})
print(df.index)2Practical Example
Here is a real-world application of df.index showing how it is used in production Pandas code.
import pandas as pd
df = pd.DataFrame({"date": ["2026-01-01", "2026-01-02"], "sales": [100, 150]})
df = df.set_index("date")
print(df.index)3Best Practices
Follow these guidelines when working with df.index:
1. Use df.set_index() to promote a meaningful column into the index, rather than manually reading its values and reassigning df.index
2. Check df.index's dtype and values after loading or transforming data, the same inspection habit as checking df.dtypes for columns
3. Use df.reset_index() to move the current index back into a regular column and restore the default RangeIndex, when a custom index is no longer needed
Tip: Use df.set_index('column_name') to promote an existing column into the DataFrame's index, which drops it as a regular column by default, rather than manually reading that column's values and assigning them to df.index yourself.
import pandas as pd
df = pd.DataFrame({"name": ["Alice", "Bob"]})
print(df.index)