Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Set the First Column as the Index in Pandas

Use df.set_index(df.columns[0]) for a loaded DataFrame, or index_col=0 when importing a CSV. Learn when the source column is dropped and how to keep it.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a DataFrame already loaded in Python, use df = df.set_index(df.columns[0]). To set the first column as the index while loading a CSV, use pd.read_csv("data.csv", index_col=0). The first approach uses the column’s position regardless of its name; the second applies that choice during import.

Set the first column as the index in an existing DataFrame

Use DataFrame.set_index with the label of the first column, which you can get by position from df.columns:

As an Amazon Associate I earn from qualifying purchases.

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

This returns a DataFrame with the selected column used for row labels. By default, drop=True, so that column is removed from the ordinary data columns. Assign the result back to df; otherwise, the original DataFrame remains unchanged.

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

Keep the source column in the data

To use the first column as the index and also retain it as a regular column, set drop=False:

df = df.set_index(df.columns[0], drop=False)

Use a column name when it is known

If the column is specifically named id, naming it directly makes the code independent of column order:

df = df.set_index("id")

You can also pass inplace=True to modify the existing DataFrame, but that call returns None; do not assign its result back to df.

Set the first column as the index when reading a CSV

Pass the zero-based column position 0 as index_col to pandas.read_csv:

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

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

This reads the CSV and uses its first column as the index, without first loading that column as ordinary data and then converting it.

If a malformed CSV has extra delimiters at the ends of lines and you do not want pandas to treat the first column as the index, the read_csv documentation describes index_col=False for that case.

Use more than one column for the index

If rows are identified by a combination of the first two columns, pass both positions. This creates a MultiIndex, so use it only when the combination reflects the row labels you intend:

# Existing DataFrame
df = df.set_index([df.columns[0], df.columns[1]])

# CSV import
df = pd.read_csv("data.csv", index_col=[0, 1])

set_index also accepts column names or a list of keys. See the pandas indexing guide for the broader method overview.

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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Importing from Excel

read_excel accepts index_col, including a zero-based column position. However, pandas documents that missing values in an index column may be forward-filled to support round-tripping files with merged cells. If that fill would alter values you need to preserve, read the sheet first and then call set_index. The pandas I/O guide covers CSV and Excel behavior.

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.

Leave a Reply

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

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.