6 Ways to Convert List to Dataframe in Python [With Examples]

We will learn about 6 Basic Ways to Convert List to Dataframe in Python with code examples. In this article we will also discuss This post explains how to generate a pandas data frame in Python from a list. So, let’s get started!

What is list in Python

In Python, a list is a mutable collection of any number of different things in sequence. All of the elements of the list are enclosed in square brackets[, separated by a comma]. The items may be of several data kinds; for example, a list may contain integer, float, and string entries. A nested list is one that has another list as one of its items.

A simple list in Python 

python_list = [1, 2, 3, 4, 5]

A nested python list

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

If you want to learn more about python lists then click on the link and read our python list article Python List.

What is a DataFrame?

Pandas is a data manipulation and analysis software library for the Python programming language. Pandas Dataframe is a possibly heterogeneous two-dimensional size-mutable tabular data format with labeled axes (rows and columns). A data frame is a two-dimensional data structure in which data is organized in rows and columns in a fairly tabular way. The data, rows, and columns are the three main components of a Pandas Dataframe.

Example:

import pandas as pd

# list of strings

list = ['Softhunt.net', 'Learn', 'coding', 'skills']
df = pd.DataFrame(list)
print(df)

Output:

List to Dataframe basic Example

Convert List to DataFrame in Python

There are several methods for creating a data frame from a list. We’ll look at six alternative Python methods for converting lists from data frames. Let’s take a look at each one individually using the examples:

Method 01: Basic method of Converting List to Dataframe

This is the simplest method to create the data frames from the list.

Example:

# import pandas as pd 

import pandas as pd 
# list of strings 
list = ['Softhunt.net', 'Learn', 'coding', 'skills']
# Calling DataFrame constructor on list 
df = pd.DataFrame(list) 
print(df) 

Output:

List to Dataframe basic Example 2

Method 02: Using a list with index and column names

We can create the data frame by giving the name to the column and index the rows

Example:

# import pandas as pd 

import pandas as pd 
# List1 
list = [['apple', 'red', 44], ['grape', 'green', 33], ['orange', 'orange', 22], ['mango', 'yellow', 11]] 
df = pd.DataFrame(list, columns =['Fruits', 'Color', 'Value'], dtype = float) 
print(df) 

Output:

index and column names

Method 03: Converting List to Dataframe Using zip() function

We can create the data frame by zipping two lists.

Example:


import pandas as pd 
# list of strings 
list1 = ['Softhunt.net', 'Learn', 'coding', 'skills']
# list of int 
list2 = [11, 22, 33, 44] 
# Calling DataFrame after zipping both lists, with columns specified 
df = pd.DataFrame(list(zip(list1, list2)), columns =['key', 'value']) 
print(df) 

Output:

Using zip() function

Method 04: Creating from the multi-dimensional list

We can create a data frame using multi-dimensional lists.

Example:

 # import pandas as pd 

import pandas as pd 
# List1 
list = [['Softhunt.net', 11], ['Learn', 22], ['coding', 33], ['skills', 44]] 
df = pd.DataFrame(list, columns =['key', 'values']) 
print(df) 

Output:

Creating from the multi-dimensional list

Method 05: Using a multi-dimensional list with column name

We can create the data frames by specifying the column name and dtype of them.

Example:

  # import pandas as pd 

import pandas as pd 
# List1 
list = [['apple', 'red', 44], ['grape', 'green', 33], ['orange', 'orange', 22], ['mango', 'yellow', 11]] 
df = pd.DataFrame(list, columns =['Fruits', 'Color', 'Value'], dtype = float) 
print(df) 

Output:

multi-dimensional list with column name

Method 06: Using a list in the dictionary

We can create data frames using lists in the dictionary.

Example:

# import pandas as pd 

import pandas as pd 
# list of name, degree, score 
n = ["apple", "grape", "orange", "mango"] 
col = ["red", "green", "orange", "yellow"] 
val = [44, 33, 22, 11] 
# dictionary of lists 
dict = {'fruit': n, 'color': col, 'value': val}  
df = pd.DataFrame(dict) 
print(df) 

Output:

Using a list in the dictionary

Conclusion

When working with a huge amount of data, it’s critical to transform the data into a format that’s easy to interpret and manipulate. Pandas dataframe are an extremely convenient way to retrieve data quickly and effectively. As we all know, data in Python is typically presented as a List, and it is critical to transforming this list into a dataframe.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. How to use Python flatten List in 10 different ways
  2. Python List

Leave a Comment