Write the first row of a Pandas DataFrame to a list

In today’s quick tutorial we’ll quickly find out how to extract the first row of a Pandas DataFrame to a list.

Create our example DataFrame

We will get started by quickly importing the Pandas library and creating a simple DataFrame that we can use for this example.

import pandas as pd

# Define data using lists
month = ['June', 'November', 'December', ]
language = ['R', 'Swift', 'Ruby', ]
first_interview = [71, 74, 76]
second_interview = [68, 53, 56]

#Constructing the DataFrame
hr_data = dict(language=language, interview_1=first_interview, interview_2=second_interview)
hr_df = pd.DataFrame(data=hr_data, index=month)

Get the first row of a Pandas DataFrame

To look into the first row of our data we’ll use the head function:


Exporting the first DataFrame row as list

Several options here, we’ll focus on using the iloc and loc indexers.

Using iloc to fetch the first row by integer location (in this case 0):

first_rec = hr_df.iloc[0]

Using loc to select the first row using its label:

first_rec = hr_df.loc['June']

In both cases the first row values will be retrieved into a Pandas Series. We can then using the to_list() method to export the Series to a list:


And our result will be:

['R', 71, 68]

Exporting the first record to an array

We can use the to_numpy function in order to retrieve the row values to a Numpy array:


#This will result in

array(['R', 71, 68], dtype=object)

Get first DataFrame column to a list

For completeness, i have added a simple snippet that uses the iloc indexer to export the first column (location = 0) to a list.


Additional learning

How to export DataFrame columns to a list?