🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.index

AI & DATA SCIENCE // df-index

df.index is the Index object holding a DataFrame's row labels, readable directly and assignable to replace all row labels at once.

Syntax

df.index

Deep Dive Course

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.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"]})
print(df.index)
localhost:3000

2Practical Example

Here is a real-world application of df.index showing how it is used in production Pandas code.

editor.html
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)
localhost:3000

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.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"]})
print(df.index)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"]})
print(df.index)
Example 02Advanced Example
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)

Best Practices

  • Use df.set_index() to promote a meaningful column into the index, rather than manually reading its values and reassigning df.index
  • Check df.index's dtype and values after loading or transforming data, the same inspection habit as checking df.dtypes for columns
  • 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

Interview Question

What's the difference between df.set_index('column') and directly assigning df.index equal to that same column's values?

Hint: Think about what happens to the original column in each approach.

df.set_index('column') both promotes that column's values into the DataFrame's index and removes it as a regular column by default, so the data isn't duplicated between the index and a column of the same name. Directly assigning df.index to that column's values instead only replaces the index, leaving the original column still present as regular data too, so the same values effectively exist twice: once as the index and once as an ordinary column, unless you also drop that column yourself afterward.

Exercises

MediumPractice using df.index in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"]})
print(df.index)

Frequently Asked Questions

What's the difference between df.set_index('column') and directly assigning df.index equal to that same column's values?

df.set_index('column') both promotes that column's values into the DataFrame's index and removes it as a regular column by default, so the data isn't duplicated between the index and a column of the same name. Directly assigning df.index to that column's values instead only replaces the index, leaving the original column still present as regular data too, so the same values effectively exist twice: once as the index and once as an ordinary column, unless you also drop that column yourself afterward.

Related Functions

pd-indexdf-columnsdf-drop