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

How to Create 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

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

  1. Using pd.DataFrame()
  2. 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

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

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

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

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!

Post Views: 125
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.

How to Append 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