Convert DataFrame to a List of Records

To convert given DataFrame to a list of records (rows) in Pandas, call to_dict() method on this DataFrame and pass ‘records’ value for orient parameter.

In this tutorial, we will learn how to use DataFrame.to_dict() method to convert given DataFrame to a list of records.

Example

In the following program, we take a DataFrame with two columns and three records. We convert this DataFrame to a list of records.

Example.py

</>
Copy
import pandas as pd

data = {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}
df = pd.DataFrame(data)

result = df.to_dict(orient='records')

print(result)

Output

[{'name': 'apple', 'quant': 40}, {'name': 'banana', 'quant': 50}, {'name': 'cherry', 'quant': 60}]

Each record is a dictionary, where the values can be accessed using column names as key.

Conclusion

In this Pandas Tutorial, we learned how to convert a DataFrame to a list of records using pandas DataFrame.to_dict() method.