python
pandas
data-analysis
dataframe
tutorial

Pandas: get the first row value of a column

Codemia Knowledge Hub ·

Use df["column"].iloc[0]. It selects the column, then takes the first row by position, regardless of what the index labels are.

Position-based access with iloc

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "name": ["Alice", "Bob", "Carla"],
6        "score": [91, 88, 95],
7    }
8)
9
10first_name = df["name"].iloc[0]
11print(first_name)

This prints Alice. Because iloc works by position, not by index label, it gives the first row even when the index labels are unusual.

Scalar access with iat

iat is optimized for reading a single cell by row and column position:

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "name": ["Alice", "Bob", "Carla"],
6        "score": [91, 88, 95],
7    }
8)
9
10column_index = df.columns.get_loc("name")
11first_name = df.iat[0, column_index]
12print(first_name)

It is a little less readable because you have to look up the column position first. For most analysis code, df["name"].iloc[0] is the better choice: clarity matters more than shaving a tiny amount of overhead from one lookup, especially after filters and sorts have changed what "first row" means.

Why loc[0] is not the same

loc uses labels. df.loc[0, "name"] only works when the index actually contains the label 0:

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {"name": ["Alice", "Bob"]},
5    index=["row_a", "row_b"],
6)
7
8print(df["name"].iloc[0])      # Alice
9print(df.loc["row_a", "name"]) # Alice

Use iloc for "first row by position" and loc for "row with this label".

Guarding against an empty DataFrame

On an empty DataFrame, iloc[0] raises IndexError. Check first when the data may be filtered down to nothing or comes from an external source:

python
1import pandas as pd
2
3df = pd.DataFrame({"name": []})
4
5if not df.empty:
6    print(df["name"].iloc[0])
7else:
8    print("No rows available")

First row vs first non-null value

If you actually want the first value that is not missing, drop nulls before taking the first one:

python
first_valid_name = df["name"].dropna().iloc[0]

Only do this when skipping nulls is really what you want, because it no longer returns the first row.

Mistakes to avoid

  • Treating iloc[0] as a label lookup. It means the first row by position, not the row labeled 0.
  • Using loc[0, "name"] on a DataFrame whose index does not contain the label 0. It fails.
  • Reading row 0 from an empty DataFrame, which raises an error.
  • Reaching for iat by default. It is fast for one cell, but df["name"].iloc[0] is usually easier to read.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design