Extract Values of First Row in pandas DataFrame in Python (Example Code)

In this tutorial you’ll learn how to return the values of the first row of a pandas DataFrame in Python programming.

Setting up the Example

import pandas as pd                            # Import pandas library to Python
my_df = pd.DataFrame({'A':[1, 1, 1, 1, 1],    # Construct example DataFrame
                      'B':[2, 2, 2, 2, 2],
                      'C':[3, 3, 3, 3, 3]})
print(my_df)                                  # Display example DataFrame in console
#    A  B  C
# 0  1  2  3
# 1  1  2  3
# 2  1  2  3
# 3  1  2  3
# 4  1  2  3

Example: Get Values in First Row of pandas DataFrame

print(my_df.iloc[0])                          # Return first row
# A    1
# B    2
# C    3
# Name: 0, dtype: int64

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top