Create pandas dataframe from lists using zip (original) (raw)

Last Updated : 07 Dec, 2022

One of the way to create Pandas DataFrame is by using zip() function. You can use the lists to create lists of tuples and create a dictionary from it. Then, this dictionary can be used to construct a dataframe. zip() function creates the objects and that can be used to produce single item at a time. This function can create pandas DataFrames by merging two lists. Suppose there are two lists of student data, first list holds the name of student and second list holds the age of student. Then we can have,

Python3

Name = [ 'tom' , 'krish' , 'nick' , 'juli' ]

Age = [ 25 , 30 , 26 , 22 ]

Above two lists can be merged by using list(zip()) function. Now, create the pandas DataFrame by calling pd.DataFrame() function.

Python3

import pandas as pd

Name = [ 'tom' , 'krish' , 'nick' , 'juli' ]

Age = [ 25 , 30 , 26 , 22 ]

list_of_tuples = list ( zip (Name, Age))

list_of_tuples

Output:

Python3

df = pd.DataFrame(list_of_tuples, columns = [ 'Name' , 'Age' ])

df

Output:

Similar Reads