How to convert lists to dataframes in Python?

Create Dataframes From Lists In Python

Hello Geeks! In this tutorial, we are going to discuss different ways to create DataFrames from lists in Python.

Also read: How to combine DataFrames in Python?


What are lists and DataFrames in Python?

In Python, lists are one of the standard data structures of Python which is widely used as an array of other Python objects. DataFrames are third-party Python objects provided by the pandas Python module that stores the data in a tabular form. A Pandas DataFrame can be created from different Python objects like lists, dictionaries, NumPy arrays, etc. But here we will limit our discussions only to the creation of pandas DataFrame objects from Python lists. So let’s install the pandas Python module and start our discussions.

C:\Users\Guest> pip install pandas

In Python, we have multiple ways to create pandas DataFrames from lists. But here we will discuss only some of them which are generally used.

1. Create DataFrame from a 1-Dimensional list

This is one of the simplest techniques to create a pandas DataFrame object Python list. In this method, we will create a 1-D list containing few elements and pass it to the DataFrame() function. This returns a pandas DataFrame object made from the data elements of the passed 1-D list.

Let’s write Python code to create a pandas DataFrame from a 1-D list.

# Import pandas module
import pandas as pd 

# Create a Python 1-D list
lst = ['Android', 'Bootstrap', 'Csharp', 'Django']

# Create a DataFrame using 1-D list
df = pd.DataFrame(lst)
print(df)

Output:

DataFrame From 1-D List

2. Create DataFrame from 2-Dimensional list (list of lists)

In this method, we create a 2-D Python list containing some elements and pass it to the DataFrame() function. Further this DataFrame() function returns a pandas DataFrame object made from the data elements of the passed 2-D list. Let’s see the Python code to create a pandas DataFrame from a 2-D list.

# Import pandas module
import pandas as pd 

# Create a Python 2-D list
lst = [['Anurag', 'Kumar', 25], 
       ['Binod', 'Rathi', 36], 
       ['Ravi', 'Yadav', 27], 
       ['Divya', 'Singh', 28], 
       ['Sanjay', 'Kumar', 21]]

# Create a DataFrame using 2-D list
df = pd.DataFrame(lst, columns = ['first_name', 'last_name', 'age'])
print(df)

Output:

DataFrame From 2-D List

3. Create DataFrame from list of tuples

We can also create pandas DataFrame objects using a Python list of tuples. Again we have three ways to create a pandas DataFrame from a list of tuples. Let’s discuss them one by one.

Method 1: Pass the list of tuples to the DataFrame() function

In this method, we simply create a Python list that contains the data in the form of Python tuples. Then we’ll pass it to the DataFrame() function. The DataFrame() function then returns a Pandas DataFrame object made from the data elements of the list of tuples.

Let’s implement this through Python code.

# Import pandas module
import pandas as pd 

# Create a Python list of tuples
list_tp = [('A1', 'A2', 'A3', 'A4', 'A5'),
          ('B1', 'B2', 'B3', 'B4', 'B5'),
          ('C1', 'C2', 'C3', 'C4', 'C5'),
          ('D1', 'D2', 'D3', 'D4', 'D5')]

# Create a DataFrame using list of tuples
df = pd.DataFrame(list_tp)
print(df)

Output:

DataFrame From List Of Tuples

Method 2: Using the from_records() function

In this method, we create a list that contains the data in the form of Python tuples just like we created in the above Python code. And pass it to the from_records() function which then returns a Pandas DataFrame object made from the data elements of the list of tuples. Let’s write the Python code to implement this.

# Import pandas module
import pandas as pd 

# Create a Python list of tuples
list_tp = [('Sanjay Kumar', 'ECE', 8.15),
           ('Uttam Kumar', 'IPE', 7.83),
           ('Ravi Kumar', 'CSE', 9.12),
           ('Anurag Singh', 'ICE', 8.88)]

# Create a DataFrame using list of tuples
# and from_records() function
df = pd.DataFrame.from_records(list_tp, columns = ['Roll', 'Program', 'CGPA'])
print(df)

Output:

DataFrame From List Of Tuples Using From Records

Method 3: Using the list() and zip() functions

In this method, we create a Python list of tuples that contains the data in the form of Python tuples using the list() and the zip() functions.

The zip() function zips the data elements from both the lists passed to it and creates a Python tuple object.

And the list() function creates a Python list of tuples from the iterator object returned by the Python zip() function.

Then we simply pass this list of tuples to the DataFrame() function which then returns a Pandas DataFrame object made from the data elements of the list of tuples. Let’s see the Python code to implement this.

# Import pandas module
import pandas as pd 

# Create two Python lists
prog_lang = ['C++', 'Python', 'Matlab', 'Csharp']
file_extn = ['.cpp', '.py', '.mat', '.cs']

# Create a Python list of tuples
# using above lists and zip() function
list_tp = list(zip(prog_lang, file_extn))

# Create a DataFrame from lists using dictionary
df = pd.DataFrame(list_tp, columns = ['Language', 'Extension'])
print(df)

Output:

DataFrame From List Of Tuples Using Zip

Conclusion

In this tutorial, we have learned the following things:

  • What are DataFrame objects and lists in Python
  • How to create a DataFrame from 1-D
  • How to create a DataFrame from list of lists or 2-D list
  • Different ways to create a DataFrame from list of tuples