Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a DataFrame already loaded in memory, use df = df.set_index(df.columns[0]). When loading a CSV, use pd.read_csv("data.csv", index_col=0). The first approach changes an existing DataFrame; the second sets the index as part of the import.

Set the first column as the index in an existing DataFrame

Select the first column by position with df.columns[0], then pass it to set_index:

df = df.set_index(df.columns[0])

This works regardless of the column’s name. By default, set_index removes that column from the regular data columns after using its values as row labels. Assign the result back to df because the method returns a new DataFrame by default.

Keep the column in the data as well

Set drop=False if you want the first column to remain among the DataFrame’s columns while also becoming its index:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
df = df.set_index(df.columns[0], drop=False)

Use a column name when it is known

If the column has a known name, specifying it directly is clearer and does not rely on its current position:

df = df.set_index("id")

Set the first column as the index while reading a CSV

Pass index_col=0 to read_csv. The zero refers to the first column’s position:

import pandas as pd

df = pd.read_csv("data.csv", index_col=0)

index_col can also take a column label. If a CSV has extra delimiters at line ends and you do not want pandas to interpret the first column as the index, the documented option is index_col=False. See the official pandas read_csv API reference.

Use more than one column for the index

When two fields together define a row label, pass both columns as a list. Pandas creates a MultiIndex:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
# Existing DataFrame: use its first two columns
df = df.set_index([df.columns[0], df.columns[1]])

# CSV: use its first two columns during import
df = pd.read_csv("data.csv", index_col=[0, 1])

Choose multiple columns only when their combination represents the row labels you need. The DataFrame.set_index API reference and read_csv reference document list-based index selection.

What to know about the set_index result

The method also has an inplace option. With inplace=True, it changes the calling DataFrame and returns None, so do not assign that return value to df. For example:

df.set_index(df.columns[0], inplace=True)

The API reference for pandas 3.0.6 marks verify_integrity deprecated since pandas 3.0.0. Check the documentation for the pandas version installed in your project before relying on version-specific parameters. See the current set_index API documentation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Excel files: consider setting the index after import

read_excel accepts index_col, including a zero-based column position. However, its documentation notes that missing values in index columns may be forward-filled to support roundtripping with merged cells. If that behavior would alter your data in an unwanted way, read the sheet first and then call set_index. The pandas I/O guide covers CSV and Excel import behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.