The Wayback Machine - https://web.archive.org/web/20240718210835/https://www.geeksforgeeks.org/creating-a-pandas-dataframe-using-list-of-tuples/
Open In App

Creating a Pandas dataframe using list of tuples

Last Updated : 09 May, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Pandas is famous for data manipulation in Python. We can create a DataFrame from a list of simple tuples, and can even choose the specific elements of the tuples we want to use.

Using pd.DataFrame() function

Here we will create a Pandas Dataframe using a list of tuples with the pd.DataFrame() function.

Example 1: In this example, we will simply pass the tuple to the DataFrame constructor which will return a pandas dataframe.

Python
# import pandas to use pandas DataFrame
import pandas as pd

# data in the form of list of tuples
data = [('Peter', 18, 7),
        ('Riff', 15, 6),
        ('John', 17, 8),
        ('Michel', 18, 7),
        ('Sheli', 17, 5) ]

# create DataFrame using data
df = pd.DataFrame(data, columns =['Name', 'Age', 'Score'])

print(df)

Output:

     Name  Age  Score
0 Peter 18 7
1 Riff 15 6
2 John 17 8
3 Michel 18 7
4 Sheli 17 5

Using from_records()

Here we will create a Pandas Dataframe using a list of tuples using the from_records() method.

Example 2: In this example, we will use the df.from_records() to create the dataframe from the list of tuples.

Python
import pandas as pd

# data in the form of list of tuples
data = [('Peter', 18, 7),
        ('Riff', 15, 6),
        ('John', 17, 8),
        ('Michel', 18, 7),
        ('Sheli', 17, 5) ]


# create DataFrame using data
df = pd.DataFrame.from_records(data, columns =['Team', 'Age', 'Score'])

print(df) 

Output:

     Team  Age  Score
0 Peter 18 7
1 Riff 15 6
2 John 17 8
3 Michel 18 7
4 Sheli 17 5


Using df.pivot() function

Here we will create a Pandas Dataframe using a list of tuples using the df.pivot() function method.

Example 3: Creating pivot table by using three columns

Python
# import pandas to use pandas DataFrame
import pandas as pd

# data in the form of list of tuples
data = [('Peter', 18, 7),
        ('Riff', 15, 6),
    ('John', 17, 8),
    ('Michel', 18, 7),
    ('Sheli', 17, 5) ]


# create DataFrame using data
df = pd.DataFrame(data, columns =['Team', 'Age', 'Score'])

a = df.pivot(index='Team', columns='Score', values='Age')
print(a)

Output:

Score      5     6     7     8
Team
John NaN NaN NaN 17.0
Michel NaN NaN 18.0 NaN
Peter NaN NaN 18.0 NaN
Riff NaN 15.0 NaN NaN
Sheli 17.0 NaN NaN NaN


Previous Article
Next Article

Similar Reads

Creating Pandas dataframe using list of lists
In this article, we will explore the Creating Pandas data frame using a list of lists. A Pandas DataFrame is a versatile 2-dimensional labeled data structure with columns that can contain different data types. It is widely utilized as one of the most common objects in the Pandas library. There are various methods for Creating a Pandas data frame us
4 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
Creating views on Pandas DataFrame
Many times while doing data analysis we are dealing with a large data set, having a lot of attributes. All the attributes are not necessarily equally important. As a result, we want to work with only a set of columns in the dataframe. For that purpose, let's see how we can create views on the Dataframe and select only those columns that we need and
2 min read
Creating views on Pandas DataFrame | Set - 2
Prerequisite: Creating views on Pandas DataFrame | Set - 1 Many times while doing data analysis we are dealing with a large data set has a lot of attributes. All the attributes are not necessarily equally important. As a result, we want to work with only a set of columns in the dataframe. For that purpose, let's see how we can create views on the D
2 min read
Creating a dataframe from Pandas series
Series is a type of list in Pandas that can take integer values, string values, double values, and more. But in Pandas Series we return an object in the form of a list, having an index starting from 0 to n, Where n is the length of values in the series. Later in this article, we will discuss Dataframes in pandas, but we first need to understand the
5 min read
Python | Creating a Pandas dataframe column based on a given condition
While operating on data, there could be instances where we would like to add a column based on some condition. There does not exist any library function to achieve this task directly, so we are going to see how we can achieve this goal. In this article, we will see how to create a Pandas dataframe column based on a given condition in Python. Proble
8 min read
Create PySpark DataFrame from list of tuples
In this article, we are going to discuss the creation of a Pyspark dataframe from a list of tuples. To do this, we will use the createDataFrame() method from pyspark. This method creates a dataframe from RDD, list or Pandas Dataframe. Here data will be the list of tuples and columns will be a list of column names. Syntax: dataframe = spark.createDa
2 min read
Convert PySpark dataframe to list of tuples
In this article, we are going to convert the Pyspark dataframe into a list of tuples. The rows in the dataframe are stored in the list separated by a comma operator. So we are going to create a dataframe by using a nested list Creating Dataframe for demonstration: C/C++ Code # importing module import pyspark # importing sparksession from pyspark.sq
2 min read
Difference Between Spark DataFrame and Pandas DataFrame
Dataframe represents a table of data with rows and columns, Dataframe concepts never change in any Programming language, however, Spark Dataframe and Pandas Dataframe are quite different. In this article, we are going to see the difference between Spark dataframe and Pandas Dataframe. Pandas DataFrame Pandas is an open-source Python library based o
3 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). This data structure can be converted to NumPy ndarray with the help of the DataFrame.to_numpy() method. In this article we will see how to convert dataframe to numpy array. Syntax of Pandas DataFrame.to_numpy()
3 min read
Practice Tags :
three90RightbarBannerImg