How to Convert a List to Pandas DataFrame?

To store the collection of multiple values of different types in a single variable, the “List” is used in Python. While storing the data in rows and columns, the 2-dimensional data structure named “DataFrame” is used. Python allows us to convert a list into a Pandas DataFrame to store the list data in a well-organized way.

This post will provide a clear guide on converting a list to pandas DataFrame.

Method 1: Using pd.DataFrame() to Convert List to Pandas DataFrame

The “pd.DataFrame()” function of the Python pandas module is used to convert the 1-dimension list into a 2-dimensional DataFrame with rows and columns.

Example 1: Convert a List to Pandas DataFrame

In the example given below, the “pd.DataFrame()” function is used to convert the list to pandas DataFrame:

Code:

import pandas
list_value = ['Joseph', 'Anna', 'Henry', 'John']
output = pandas.DataFrame(list_value, columns=['Name'])
print(output)

In the above code:

  • The “pd.DataFrame()” function accepts the list as a first argument, and the column name of the given list is passed as a second argument. 

Output:

The above output verified that the “list” had been converted into DataFrame.

Example 2: Convert List of List to Pandas DataFrame

In the below example, the “List of List” or nested list is converted into pandas DataFrame using the “pd.DataFrame()” function:

Code:

import pandas
list_value = [['Joseph', 15], ['Lily', 18],['Henry', 18], ['Anna', 12]]
output = pandas.DataFrame(list_value, columns=['Name', 'Age'])
print(output)

In the above code:

  • The nested list is created and passed as an argument to the “pd.DataFrame()” function.
  • The “pd.DataFrame()” function converts the input nested list into pandas DataFrame.

Output:

The above output shows that the nested list or list of lists has been converted into Pandas DataFrame.

Example 3: List as a Dictionary Value Converted into Pandas DataFrame

The dictionary containing multiple key values in the form of a list can also be converted into pandas DataFrame using the “pd.DataFrame()” function.

Code:

import pandas
list_dict = {'Name': ['Joseph', 'Anna', 'Henry', 'John'],
            'Age': [22, 32, 21, 12]}
output = pandas.DataFrame(list_dict)
print(output)

In the above code:

  • The dictionary key “Name” and “Age” is initialized with multiple values in the form of a list.
  • The “pd.DataFrame()” function accepts the dictionary variable “list_dict” as an argument and returns the DataFrame.

Output:

The dictionary containing the key value as a list has been converted into DataFrame in the above output.

Method 2: Using zip() Function

The “zip()” function in Python creates a zip object by accepting two or more iterators as an argument. In the below code, the “zip()” function is used along with the “pandas.DataFrame()” function to convert a list to DataFrame:

Code:

import pandas
list_value = ['Joseph', 'Anna', 'Henry', 'John']
list_value1 = [22, 32, 21, 12]
output = pandas.DataFrame(zip(list_value,list_value1), columns=['Name', 'Age'])
print(output)

In the above code:

  • The “pd.DataFrame()” function accepts the zip object and returns the Pandas DataFrame with specific columns “Age” and “Name”.

Output:

The above output shows that the given list has been converted into Pandas DataFrame.

Conclusion

To convert a list to pandas DataFrame, the “pd.DataFrame()” function is used in Python. The “pd.DataFrame()” function can also be used along with the “zip()” function to convert the list to pandas DataFrame. The nested list or list of lists and the dictionary key value as a list can also be converted into pandas DataFrame using the “pd.DataFrame()” function. This guide presented an in-depth overview of how to convert a list to pandas DataFrame via practical demonstration.