Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Creating Pandas DataFrame From List of Tuples in Python

  • 22 Jan, 2025
  • Com 0
Featured Image of Creating Pandas DataFrame From List of Tuples in Python

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.

Here are two ways to create a DataFrame from a list of tuples:

  1. Using pd.DataFrame()
  2. Using from_records()

Method 1: Using pd.DataFrame()

The most common way to create a DataFrame in Pandas from any type of structure, including a list, is the .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

Using pd.DataFrame() to create a DataFrame from a list of tuples

Here, from the above image, you can see that each tuple of the list represents the individual rows of the data frame.

While creating a data frame, we pass the “columns” argument that will create columns in the final DataFrame.

Pros

  1. The .DataFrame() constructor not only works with lists but also works well with tuples, dictionaries, or numpy arrays. You can create a DataFrame out of any data structure.
  2. It requires less coding, which makes it very easy to implement and understand.
  3. It is an efficient and idiomatic way to create a DataFrame.

Cons

  1. It becomes less efficient once the list is really large.
  2. If you have record-like data, it has the least advantage in it.

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

Using from_records() to create a DataFrame from a list of tuples

You can see from the above image that almost everything is the same as the first approach, and it returns a DataFrame with proper rows and columns.

Pros

  1. The from_records() method is intentionally designed for record-like data, which makes it very efficient.
  2. It handles numpy arrays extremely well if your data structure is that.

Cons

  1. It can become more verbose than .DataFrame() constructor.
  2. It is less versatile when working with different types of data.

That’s it!

Post Views: 49
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Appending a New Row to an Empty Array in Numpy
How to Use Ajax in Laravel 11

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend