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.
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.
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.
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.
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.
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 labeled0.' - Empty DataFrames raise an error when you try to read row
0. - '
iatis fast for one cell, butdf["name"].iloc[0]is usually easier to read.'
Summary
- Use
df["column"].iloc[0]for the most readable first-row-by-position lookup. - Use
iatwhen you want fast scalar access by row and column position. - Use
loconly when you mean an index label, not the first row position. - Check for an empty DataFrame when the input may contain no rows.

