HOWTO · Pandas

How to Get the First Row of DataFrame Pandas

Learn to select the first row of a Pandas DataFrame with iloc, head(), filtering, query(), take(), and slicing, and choose a Series or DataFrame result.

On this page

Use df.iloc[0] to retrieve the first row as a Series, or df.head(1) to keep it as a one-row DataFrame. For the first row matching criteria, filter first and then select the first result.

The examples use this small DataFrame so the selected values and original row labels are easy to follow. Each code block is self-contained and can be run in a Python environment with pandas installed; the observed output from the supplied runtime run is shown after each example.

Get the First Row of a Pandas DataFrame Using the pandas.DataFrame.iloc Property

pandas.DataFrame.iloc selects rows and columns by integer position. Position 0 means the first row regardless of its index label. Selecting one row with df.iloc[0] returns a Series, whose index contains the DataFrame’s column names. Use df.iloc[0, 1] to select a single value at row position 0 and column position 1.

import pandas as pd

df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 200, 350],
    }
)

first_row = df.iloc[0]
print(first_row)
C_1      A
C_2     40
C_3    430
Name: 0, dtype: object

Choose this form when a Series is convenient for later calculations or access by column name. To preserve a two-dimensional DataFrame shape, use df.iloc[[0]] or one of the methods below.

Get the First Row of a Pandas DataFrame Using the pandas.DataFrame.head() Method

pandas.DataFrame.head(n) returns the first n rows as a DataFrame. Pass 1 to keep just the first row while preserving the column structure and its index label. This is often the clearest choice when later code expects a DataFrame.

import pandas as pd

df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 200, 350],
    }
)

first_row = df.head(1)
print(first_row)
  C_1  C_2  C_3
0   A   40  430

The key choice is the result shape: df.iloc[0] is one-dimensional, while df.head(1) is still a DataFrame.

Get the First Row of a Pandas DataFrame Based on the Specified Condition

Filter rows before selecting the first match. In this example, the condition is C_2 < 40 and C_3 > 450; the first matching row is the one labeled 1. The boolean mask uses parentheses around each comparison because & combines the two conditions.

import pandas as pd

df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 500, 350],
    }
)

matches = df[(df["C_2"] < 40) & (df["C_3"] > 450)]
first_match = matches.head(1)
print(first_match)
  C_1  C_2  C_3
1   B   34  980

head(1) returns an empty DataFrame if no row matches. If you need a single Series when a match exists, select with matches.iloc[0]; check matches.empty first when the criteria may have no matches.

The query() method expresses the same filter as a string and can be convenient for readable column expressions. Keep the predicate aligned with the boolean-filter example:

import pandas as pd

df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 500, 350],
    }
)

matches = df.query("(C_2 < 40) & (C_3 > 450)")
first_match = matches.head(1)
print(first_match)
  C_1  C_2  C_3
1   B   34  980

For values from Python variables, use @variable in a query() expression. For example, limit = 40 can be referenced as df.query("C_2 < @limit").

Get the First Row of a Pandas DataFrame Using the pandas.DataFrame.take() Method

pandas.DataFrame.take() selects rows by position when its default row axis is used. Pass [0] to select the first row as a one-row DataFrame, retaining its original index label.

import pandas as pd

df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 200, 350],
    }
)

first_row = df.take([0])
print(first_row)
  C_1  C_2  C_3
0   A   40  430

Use take() when a list of row positions is already available. For a simple first-row selection, head(1) or iloc[[0]] may communicate intent more directly.

Get the First Row of a Pandas DataFrame Using Slicing

The slice df[:1] selects rows from the start up to, but not including, position 1. It returns a DataFrame and retains the selected row’s index label.

import pandas as pd

df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 200, 350],
    }
)

first_row = df[:1]
print(first_row)
  C_1  C_2  C_3
0   A   40  430

Conclusion

Use df.iloc[0] when you want a Series, and use df.head(1), df.iloc[[0]], df.take([0]), or df[:1] when you want a one-row DataFrame. To get the first row that meets criteria, filter with a boolean condition or query() and then select the first filtered row. Check matches.empty before indexing when no match is possible.