Get First Row of Pandas DataFrame?

  • Post author:
  • Post category:Pandas
  • Post last modified:October 30, 2023

By using DataFrame.iloc[0] and head(1) you can select/get the first row of pandas DataFrame. iloc[] is a property that is used to select rows and columns by position/index. If the position/index does not exist, it gives an index error. In this article, I will cover usage of pandas.DataFrame.iloc[] and using this how we can get the first row of Pandas DataFrame in different ways with examples.

pandas loc[] is another property that is used to operate on the column and row labels. For a better understanding of these two learn the differences and similarities between pandas loc[] vs iloc[].

1. Quick Examples of How to Get First Row of Pandas DataFrame

If you are in a hurry, below are some quick examples of how to get the first row of DataFrame.


# Below are some quick examples
 
# Example 1: Get first Row of Pandas DataFrame
print(df.iloc[0])

# Example 2: Get first row using range index
print(df.iloc[:1])

# Example 3: Get first row value using particular column
print(df['Fee'].iloc[0])

# Example 4:  Get first row value using index range
print(df['Discount'].iloc[:1])

# Example 5: Get first row using index
print(df.loc[df.index[0]])

# Example 6:  Get first row using values[]
print(df.values[:1])

# Example 7: Get first row of particular column
print(df['Fee'].values[:1])

# Example 8: Get the first row use head()
print(df.head(1))

# Example 9: Get the first row of DataFrame as a list
print(df.iloc[0].tolist())

Let’s create DataFrame using data from the Python dictionary and run the above examples to get the first row of DataFrame.


# Import pandas library
# Create pandas DataFrame
import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
    'Fee' :[20000,25000,26000,22000,24000],
    'Duration':['30day','40days','35days','40days','60days'],
    'Discount':[1000,2300,1200,2500,2000]
              }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(technologies, columns = ['Courses', 'Fee', 'Duration', 'Discount'], index = index_labels)
print(df)

# Outputs:
    Courses    Fee Duration  Discount
r1    Spark  20000    30day      1000
r2  PySpark  25000   40days      2300
r3   Hadoop  26000   35days      1200
r4   Python  22000   40days      2500
r5   pandas  24000   60days      2000

2. Get the First Row of Pandas using iloc[]

Using the Pandas iloc[] attribute we can get the single row or column by using an index, by specifying the index position 0 we can get the first row of DataFrame. iloc[0] will return the first row of DataFrame in the form of Pandas Series.

Related: You can use df.iloc[] to get the last row of DataFrame.


# Get first row using row position
print(df.iloc[0])

# Output:
# Courses      Spark
# Fee          20000
# Duration    30days
# Discount      1000
Name: r1, dtype: object 

We can also get the first row of Pandas DataFrame by providing an index range i.e.[:1] to iloc[] attribute. This syntax will select the rows from 0 to1 and returned the first row in the form of DataFrame. For example,


# Get first row using range index
print(df.iloc[:1])

# Output:
#   Courses    Fee Duration  Discount
# r1   Spark  20000   30days      1000

3. Get the First Row using loc()

We can also get the first row of DataFrame using the loc[] attribute for that, we have to pass the first row index with the help of the index[] attribute. It will return the first row in the form of Series object.


# Get first row using loc() function
print(df.loc[df.index[0]])

# Output:
# Courses     Spark
# Fee         20000
# Duration    30day
# Discount     1000
# Name: r1, dtype: object

4. Get the First Row of Pandas using values() 

Pandas DataFrame.values attribute is used to return a Numpy representation of the given DataFrame. Use this attribute we can get the first row of DataFrame in the form of NumPy array. Let’s get the first row,


# Get first row using loc() function
print(df.values[:1])

# Output:
# [['Spark' 20000 '30day' 1000]]

# Get particular column
print(df['Fee'].values[:1])

# Output:
# [20000]

5. Get the First Row of DataFrame using head()

The pandas.DataFrame.head() method returns the first n rows of dataframe. We can use this head() function to get only the first row of the dataframe, for that, we pass '1' as an argument to this function. It will return the first row of DataFrame.


# Get the first row use head()
print(df.head(1))

# Output:
#    Courses    Fee Duration  Discount
# r1   Spark  20000    30day      1000

6. Get the First Row of Pandas as a List

As we know from the above, we have got the first row of the DataFrame using df.iloc[0]. It has given the result as a series object. Using the series.tolist() function, we can get the first row of DataFrame in the form of a list. For example,


# Get the first row of DataFrame as a list
print(df.iloc[0].tolist())

# Output:
# ['Spark', 20000, '30day', 1000]

7. Get the First Row of a particular column

If we want to get value of first row based on particular column, we can pass specified column into DataFrame then call iloc[] attribute. It will return value of first row based on specified column.


# Get first row value using particular column
print(df['Fee'].iloc[0])

# Output:
# 20000

Alternatively, we can get the value of the first row based on a particular column using the index range of the iloc[] attribute. It will return the first row value in the form of a Series.


#  Get first row value using index range
print(df['Discount'].iloc[:1])

# Output:
# r1    1000
# Name: Discount, dtype: int64

Conclusion

In this article, I have explained the usage of DataFrame.iloc[] and using this how we can get the first row of DataFrame in different ways. As well as I explained how to get the first row of DataFrame using head() and other functions.

Happy Learning !!

References

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials.

Leave a Reply

You are currently viewing Get First Row of Pandas DataFrame?