Here are two ways to create a DataFrame from a list of tuples:
- Using pd.DataFrame()
- Using from_records()
Tuples are the perfect way to represent records or rows of the Data Frame, where each element in the tuple corresponds to a specific field or column.
When you are creating a DataFrame, a list of tuples represents the rows of the DataFrame. Each tuple within the list corresponds to a single row, and the elements within each tuple represent the values for the different columns in that row.
Method 1: Using pd.DataFrame()
The most common way to create a DataFrame in Pandas from any type of structure, including a list, is .DataFrame() constructor.
If the tuple contains nested tuples or lists, each nested tuple/list becomes a row in the DataFrame.
import pandas as pd
list_of_tuples = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)]
print("Before converting to data frame")
print(list_of_tuples)
df = pd.DataFrame(list_of_tuples, columns=['col1', 'col2', 'col3'])
print("After converting to data frame")
print(df)
Output
Here, from the image above, you can see that each tuple in the list represents an individual row of the data frame.
When creating a data frame, we pass the “columns” argument, which creates the columns in the final DataFrame.
Method 2: Using from_records()
The pd.DataFrame.from_records() method is specifically helpful for converting a list of tuples (or other sequences) to a DataFrame. Each tuple in the list becomes a row in the DataFrame.
import pandas as pd
list_of_tuples = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)]
print("Before converting to data frame")
print(list_of_tuples)
df = pd.DataFrame.from_records(list_of_tuples,
columns=['col1', 'col2', 'col3'])
print("After converting to data frame")
print(df)
Output
As shown in the above image, the approach is nearly identical to the first one, and it returns a DataFrame with the correct rows and columns.
That’s it!


