How to Convert a list to DataFrame in Python

In this Python tutorial, we will discuss several methods to convert a list to DataFrame in Python. Moreover, we’ll look at various examples to convert the list of tuples to a string in Python.

Recently, I am working on a machine learning project and I have found that it requires some column and row elements rather than a Python list. So I have done some research and found that we have to convert the list to a DataFrame in Python.

Here we will learn

  • How to convert a list to DataFrame in Python using dataframe()
  • Convert a list to DataFrame in Python using zip()
  • How to convert a list to DataFrame in Python using a multidimensional list
  • Convert a list to DataFrame in Python using lists in the dictionary
  • How to convert a list to DataFrame in Python using Using list with index and column names

Convert a list to DataFrame in Python

In Python, there are primarily Six methods that are commonly used and important to understand when converting a list to DataFrame in Python.

How to convert a list to DataFrame in Python using dataframe()

  • In this section, we will discuss how to convert a list to DataFrame in Python using dataframe().
  • Use Python’s pd.dataframe() method to convert a list to a dataframe. An inbuilt library function called pandas dataframe() accepts a list as a parameter and creates a dataframe from it.

Syntax:

Let’s have a look at the Syntax and understand the working of pd.dataframe() in Python.

pandas.DataFrame( data, index, columns, dtype, copy)
  • It consists of a few parameters
    • data: Data can be in many different formats, including DataFrames, ndarrays, series, maps, lists, dictionaries, constants, and more.
    • index: If no index is provided, np.arange(n) will be used as the default index for the row labels in the resulting frame.
    • columns: It displays only true if no index is passed

Example:

import pandas as pd  
# Creating the list

Country_name = ['U.S.A','Germany','Australia','China']  
# Using the pd.dataframe()
new_result = pd.DataFrame(Country_name)  
# Display the Content
print("Converted list to dataframe :",new_result)  

In the above code first, we imported the Pandas library and then created a list named “Country_name” and assign the elements.

Next, we used the pd.dataframe() and passed the list as an argument and it displays the output in the form of a dataframe.

Here is the Screenshot of the following given code

How to convert a list to DataFrame in Python using dataframe
Converting a list to DataFrame in Python using dataframe

This is how to convert a list to DataFrame in Python using dataframe.

Read: Convert string to float in Python

Convert a list to DataFrame in Python using zip()

  • Now let us see how to convert a list to DataFrame in Python using zip().
  • The built-in zip() function in Python allows users to combine any number of iterables (list, str, etc.) by taking items from each iterable that is passed as input.
  • Here we will create two lists the first is a list of strings, whereas the second is a list of integers. Our list is then passed to the dataframe function.

Syntax:

Here is the Syntax of the zip() function in Python

zip(*iterables)

Example:

Let’s take an example and check how to convert a list to DataFrame in Python using zip().

Source Code:

import pandas as pd

Cars_in_USA = ['BMW', 'Tesla', 'Volkswagen']

new_values = [674, 723, 178]

new_result = pd.DataFrame(list(zip( Cars_in_USA, new_values)), columns = ['Cars_name', 'value'])

print(new_result)

In the following given code first, we created a list and then use the zip() function within pd.dataframe() method will combine the two lists in the order of columns.

Here is the Screenshot of the following given code

Convert a list to DataFrame in Python using zip
Converting a list to DataFrame in Python using a zip

As you can see in the Screenshot we have discussed how to convert a list to DataFrame in Python using a zip.

Read: Python convert binary to decimal

How to convert a list to DataFrame in Python using a multidimensional list

  • In this section, we will discuss how to convert a list to DataFrame in Python using a multidimensional list.
  • In this example, we will create a multidimensional list and see how to convert them into a list. For this, we are going to use the pd.dataframe() and it will convert a list to a dataframe.

Example:

Here we will take an example and check how to convert a list to DataFrame in Python using a multidimensional list.

Source Code:

import pandas as pd  
#list contains integer and string values 
Bikes_in_USA = [['Harley Davidson', 7453], ['BMW', 4532], ['Sports bike', 9123]]  
new_output = pd.DataFrame(Bikes_in_USA, columns = ['Bikes_name', 'bike_number'])  
# Display the Content
print("Converted list into dataframe :",new_output) 

In the following given code first, we imported the Pandas library and then created multidimensional lists and assigned to it the pd.dataframe() function along with that we mentioned the column names.

You can refer to the below Screenshot.

How to convert a list to DataFrame in Python using multidimensional list
Converting a list to DataFrame in Python using a multidimensional list

In this example, we have understood how to convert a list to DataFrame in Python using a multidimensional list.

Read: Convert tuple to string in Python

Convert a list to DataFrame in Python using lists in the dictionary

  • Here we will discuss how to convert a list to DataFrame in Python using lists in the dictionary.
  • In this example for the dictionary’s key values, we used the input lists and then converted them into a dataframe.

Example:

Let’s take an example and check how to transform a list to DataFrame in Python using lists in the dictionary.

Source code:

import pandas as pd  
#list_values having strings  
States_in_USA = ['Alabama', 'Alaska', 'Arizona']  
zip_code = [35242,36117,36695]    
new_dict = {'U.S.A states':States_in_USA, 'Zip_code':zip_code}  
new_output = pd.DataFrame(new_dict)  
print("Converted list into dictionary :",new_output)  

In the above code first, we created the lists and assign them to a dictionary. Next, we used the pd.dataframe() function and within this function, we passed the dictionary into a list and which will be converted into a dataframe.

Here is the implementation of the following given code.

Convert a list to DataFrame in Python using lists in the dictionary
Converting a list to DataFrame in Python using lists in the dictionary

Read: Python convert dictionary to an array

How to convert a list to DataFrame in Python using Using list with index and column names

  • In this section, we will discuss how to convert a list to DataFrame in Python using Using list with index and column names.
  • Here first we will create a dataframe that has an index value and a column name and also we will use the pd.dataframe() function.

Example:

Let’s take an example and check how to convert a list to DataFrame in Python using Using list with index and column names.

Source code:

 import pandas as pd  
# Creation of list 
Country_name = ['U.S.A', 'Germany', 'Australia']  
new_result = pd.DataFrame(Country_name,index = [0,1,2], columns = ['Country_name'])  
# Display the Content 
print("Converted list to DataFrame :",new_result)    

Here is the Screenshot of the following given code.

How to convert a list to DataFrame in Python using list with index and column names
Converting a list to DataFrame in Python using a list with index and column names

Also, take a look at some more Python tutorials.

In this article, we have discussed how to convert a list to DataFrame in Python, and also we have covered the following given topics.

  • How to convert a list to DataFrame in Python using dataframe()
  • Convert a list to DataFrame in Python using zip()
  • How to convert a list to DataFrame in Python using a multidimensional list
  • Convert a list to DataFrame in Python using lists in the dictionary
  • How to convert a list to DataFrame in Python using Using list with index and column names