python
pandas
data-analysis
dataframe
tutorial

Pandas Get first row value of a given column

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want the value from the first row of one Pandas column, the clearest solution is usually df["column"].iloc[0]. That expression is explicit about both parts of the task: choose the column first, then take the first row by position.

Use iloc for Position-Based Access

iloc is the standard way to access rows and columns by integer position.

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. It works even if the DataFrame index labels are something unusual, because iloc is based on position, not on index names.

Fast Scalar Access With iat

If you only need one scalar value and want the most direct cell lookup, iat is also a good choice.

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)

iat is optimized for one scalar cell, but it is a little less readable because you have to compute the column position first.

loc Is Different

loc uses labels, not positions. That means df.loc[0, "name"] works only 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

If your real question is "first row by position," prefer iloc. If your real question is "row with a specific label," use loc.

Handle Empty DataFrames Safely

The most common failure case is an empty DataFrame. In that case, iloc[0] raises IndexError.

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")

That small guard is worth adding when the input may be filtered or loaded from an external source.

Decide Whether "First" Means Position or First Valid Value

Sometimes the real question is not "first row by position" but "first non-null value." Those are different tasks. If missing data is possible, you may need dropna() before taking the first value.

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

That is only correct when skipping nulls is what you actually want.

Readability Usually Wins

For most data-analysis scripts, df["name"].iloc[0] is the best answer because it is easy to read and easy to explain. iat is useful, but clarity usually matters more than shaving a tiny amount of access overhead from one lookup.

That matters even more after filters and sorts. If the DataFrame was transformed earlier in the pipeline, a clear positional lookup makes it easier to reason about what "first row" now means. It also makes later code review easier. It keeps the intent obvious. That is often the bigger win in analysis code. Readability compounds quickly.

Common Pitfalls

  • 'iloc[0] means first row by position, not by index label.'
  • 'loc[0, "name"] fails if the DataFrame index is not actually labeled 0.'
  • Empty DataFrames raise an error when you try to read row 0.
  • 'iat is fast for one cell, but df["name"].iloc[0] is usually easier to read.'

Summary

  • Use df["column"].iloc[0] for the most readable first-row-by-position lookup.
  • Use iat when you want fast scalar access by row and column position.
  • Use loc only when you mean an index label, not the first row position.
  • Check for an empty DataFrame when the input may contain no rows.

Course illustration
Course illustration

All Rights Reserved.