How to convert list of tuples to Dataframe in Python

How to convert list of tuples to Dataframe in Python

A tuple is a data structure that contains an ordered collection of data. It allows you to store multiple items in a single variable. You can compare it with the list. Tuple uses a round bracket instead of a square bracket like the list. You can also aggregate two or more tuples in a list. In this entire tutorial, you will learn how to convert a list of tuples to pandas dataframe in python.

Method to convert the list of tuples to dataframe

In this section, you will know all the methods for a list of tuples to dataframe conversion. But before that let’s create a sample list of tuples for demonstration purposes. Below is the line of code to create it.

tuple_list = [('a', 10), ('b', 20), ('c', 30)]
print(tuple_list)

Output

[('a', 10), ('b', 20), ('c', 30)]

Method 1: Using the pandas.DataFrame() constructor.

The simplest way to convert the list of tuples to pandas dataframe is to pass the list of tuples as an argument of it. Execute the below lines of code to convert the tuple.

import pandas as pd
tuple_list = [('a', 10), ('b', 20), ('c', 30)]
df= pd.DataFrame(tuple_list)
print(df)

Output

Using the pandas.DataFrame() constructor

Method 2: Convert the list of tuples to dataframe using the from_records() function

The second method to convert tuples to dataframe is the use of the from_record() function. Here also you have to just pass your list of tuples as an argument.

Run the following lines of code to convert to dataframe.

import pandas as pd
tuple_list = [('a', 10), ('b', 20), ('c', 30)]
df= pd.DataFrame.from_records(tuple_list)
print(df)

Output

using the from_records() function
using the from_records() function

Method 3: Using the zip() function

There is also the case when you have more than one tuple and want to convert them to dataframe. Then in this case you have to use the zip() function. It will combine each tuple into a list of tuples. After that, the process is the same as the above.

Execute the following lines of code and see the converted the dataframe from the list.

import pandas as pd
t1 = [('a', 10), ('b', 20)]
t2 = [('c', 30),('d',40)]
df = pd.DataFrame((zip([t1,t2])))
print(df)

Output

 0 1
0 a 10
1 b 20
2 c 30

Conclusion

Tuples are immutable data structures in which elements cannot be changed after their creation. If you have a list of tuples and want to convert it into dataframe then the above method will work for you. You can also add the name of the columns using the columns attribute.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner