Open In App
Related Articles

Creating a Pandas dataframe using list of tuples

Improve Article
Improve
Save Article
Save
Like Article
Like

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.

Python3




# 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.

Python3




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

Python3




# 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('Team', 'Score','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


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 25 Aug, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials