Convert Nested List to pandas DataFrame in Python (2 Examples)

 

Hi! This tutorial will show you 2 methods of converting a nested list to a pandas DataFrame in the Python programming language.

First, here is an overview of this tutorial:

Let’s dive right into it!

 

Create Nested List

Here, we will create the nested list sample that we will use in this tutorial. Therefore, run the line of code below in your Python IDE to create the nested list:

nested_list = [[1,2,3],[4,5,6],[7,8,9]]

Our nested list is called nested_list, now, let’s return a DataFrame using it!

 

Example 1: Turn Nested List to DataFrame Using DataFrame() Function

In this example, we will use the pandas DataFrame() function to convert the nested list into a DataFrame like so:

df = pd.DataFrame(nested_list, columns=["A","B","C"])
print(df)
 
#   A  B  C
#0  1  2  3
#1  4  5  6
#2  7  8  9

We parsed a list of column names to the columns = argument as the program would have numbered the columns 0, 1, and 2 instead.

 

Example 2: Turn Nested List to DataFrame Using DataFrame.from_records() Function

In this next example, we will use the pandas DataFrame.from_records() function to convert the nested list into a DataFrame, and similarly parse a list of column names to the function’s columns = argument:

df = pd.DataFrame.from_records(nested_list, columns=["A","B","C"])
print(df)
 
#   A  B  C
#0  1  2  3
#1  4  5  6
#2  7  8  9

So, there you have it! We have demonstrated in this tutorial 2 simple methods of converting a nested list to a pandas DataFrame in the Python programming language.

I hope you found this helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a nested list into a pandas DataFrame in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a nested list into a pandas DataFrame in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a nested list into a pandas DataFrame in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top