Convert List to DataFrame Python
A “list” is a collection of items that can be of different types and a “Dataframe” is like a table with rows and columns that can store data. Dataframes are useful for working with data and finding patterns, therefore sometimes there can be a need to convert a list into a dataframe to do some analysis.
This article will show you how to convert a list into a dataframe in Python utilizing different methods.
How to Convert Python List to DataFrame?
The following methods are utilized to convert/transform the Python list to “DataFrame”:
- Using “pandas.DataFrame()” Function.
- Using “numpy.reshape()” Function.
- Using “pandas.concat()” Function.
- Using “List Comprehension” and “zip()” Function.
- Using “Dictionary”.
- Using “pandas.DataFrame.from_records()” Function.
Method 1: Converting a List to Dataframe Using “pandas.DataFrame()” Function
The “pandas.DataFrame()” function is an easy way to convert the given list to the Data Frame.
Example
Overview of the below-stated code:
import pandas
df = pandas.DataFrame(list_name, columns=['Name'])
print(df)
In this code block, the “pandas” library is imported and a data frame is created using the “pandas.DataFrame()” function. This function takes the created list as an argument and converts it based on the “columns” parameter that specifies the column name of the data frame created from the list.
Output
In the above snippet, the resulting data frame has one column named “Name” comprising the list values.
Method 2: Converting List to Dataframe Using “numpy.reshape()” Function
The “numpy.reshape()” function can also be utilized to convert a Python list to a data frame.
Example
Go through the below-given example:
import numpy
list_name = ['Joseph', 'Lily', 'Anna', 'David']
arr = numpy.array(list_name)
df = pandas.DataFrame(numpy.reshape(arr, (4, 1)), columns=['Name'])
print(df)
In the above code, the “numpy.array()” function is utilized to convert the initialized list to a numpy array. After that, the “numpy.reshape()” function reshapes the array into a two-dimensional array. Lastly, the resulting array is converted to a data frame using the “pandas.DataFrame()” function.
Output
Based on the above snippet, the data frame has been retrieved by utilizing the defined list.
Method 3: Converting List to Dataframe Using “pandas.concat()” Function
The “pandas.concat()” function can also be applied to convert a list to a data frame.
Example
Here is an example of code:
list_name = ['Joseph', 'Lily', 'Anna', 'David']
df = pandas.concat([pandas.Series(list_name)], axis=1, keys=['Name'])
print(df)
This code block first creates a pandas “Series” from the given Python list by utilizing the “pandas.Series()” function. After that, the “pandas.concat()” function concatenates the series horizontally and the “keys” parameter specifies the column name of the resulting data frame, thereby performing the desired conversion.
Output
The data frame has been created by using the initialized list in the above snippet.
Method 4: Converting List to Dataframe Using “List Comprehension” and “zip()” Function
A data frame can also be constructed from the defined Python list using “List Comprehension” and the “zip()” function.
Example
Check out the below-provided code to get a better understanding:
list_name = ['Joseph', 'Lily', 'Anna', 'David']
df = pandas.DataFrame([i for i in zip(list_name)], columns=['Name'])
print(df)
In the above lines of code, the “List Comprehension” approach and the “zip()” function is used to create a list of tuples from the given list. The tuples list is then converted to a data frame utilizing the “pandas.DataFrame()” function.
Output
Method 5: Converting List to Dataframe Using “Dictionary”
The “Dictionary” data structure can also be used along with the “pd.DataFrame()” function to convert/transform the Python list to Data Frame.
Example
The following code is used to convert the given Python list to Pandas Data Frame:
list_name = ['Joseph', 'Lily', 'Anna', 'David']
dict = {'Name': list_name}
df = pandas.DataFrame(dict)
print(df)
According to the above code, the “Dictionary” is created with the “key” as “Name” and the given list as the “value”. The dictionary is then transformed into a particular data frame utilizing the “pandas.DataFrame()” function.
Output
Method 6: Converting List to Dataframe Using “pandas.DataFrame.from_records()” Function
The “pandas.DataFrame.from_records()” function can also be implemented to convert a list to a data frame.
Example
The below code block is used to apply the discussed conversion:
list_name = ['Joseph', 'Lily', 'Anna', 'David']
data = [(ele,) for ele in list_name]
df = pandas.DataFrame.from_records(data, columns=['Name'])
print(df)
In this code, we created a list of tuples from the initialized list using the “List Comprehension” approach. The tuples list is then converted to a data frame utilizing the “pandas.DataFrame.from_records()” function.
Output
Conclusion
To convert a Python list to Pandas DataFrame, various methods such as the “pandas.DataFrame()” function, “numpy.reshape()” function, “pandas.concat()” function, “List Comprehension” and “zip()” function, “Dictionary”, or the “pandas.DataFrame.from_records()” function can be used in Python. This guide explains how to transform a Python list to a Pandas DataFrame using appropriate examples.
Source: linuxhint.com