Create a DataFrame using Lists in Python

Ways to create a DataFrame using Lists in Python

We use the pandas.dataframe() function from the Pandas library to create a DataFrame. We will discuss how to create a DataFrame using Lists with this function.

A list is a linear collection and we can use it to pass such objects as the values for a column in Python to create a DataFrame.

See the code below.

#Create DataFrame using Lists in Python
import pandas as pd
# We use the pandas.DataFrame() function to create DataFrames in Python.
lst = ["Ray","Vivek","Tom"]
df_from_lst = pd.DataFrame(lst, columns = ['First Name'])
#Output
print(df_from_lst)
#   First Name
# 0        Ray
# 1      Vivek
# 2        Tom    

In the above example,

  • We create a list that contains the names of different individuals.
  • We create a DataFrame using the pandas.DataFrame() function.
  • We give the list as the value of a column of the dataframe.

We know that a DataFrame can store values in rows and columns so it will make more sense if we can use multiple lists as values for multiple columns.

To achieve this, we need to club the lists together in one object. One such object that we can use is the zip object. Such an object is created using the zip() function and can combine elements at corresponding positions of different iterable.

In simple words, an iterable is an object in Python that can be iterated over using the for loop and stores elements at specific positions.

Let us observe this with an example.

# Create DataFrame using Lists in Python
import pandas as pd
lst1 = ["Ray","Vivek","Tom"]
lst2 = [5,2,7]
# The zip() function allows us to combine the two lists into one ojbect
df_from_lst = pd.DataFrame(list(zip(lst1,lst2)), columns = ['First Name','Rank'])
print(df_from_lst)
#Output
#  First Name  Rank
#0        Ray     5
#1      Vivek     2
#2        Tom     7   

In the above example,

  • We combine two lists into a single object using the zip() function.
  • This object is then used to specify values of multiple columns in the DataFrame

Similarly, other objects like dictionaries can also be used as an alternative to the zip() function and lists for creating DataFrames.

To Conclude

This tutorial demonstrated several methods to create a dataframe using lists in Python. The DataFrame() function of the Pandas library is used to create such objects. The list is specified as the value for a column in the DataFrame. We also demonstrated the use of zip() function to create a dataframe using lists in Python.

Explore more from this category at Python DataFrame. Alternatively, search and view other topics at All Tutorials.