In this Python tutorial, we will discuss several methods to convert a list to DataFrame in Python.
There are nine methods to convert a list to a dataframe in Python, which are shown below:
- Using the pandas.DataFrame() function
- Using column names and index
- Using zip() function
- Using the multidimensional list
- Using a list in the dictionary
- Using a multi-dimensional list with dtype and column name specified
- Using the from_records() function
- Using the from_dict() function
- Using the list of tuples
Convert a list to DataFrame in Python
Sometimes it is useful to convert a list to a DataFrame in order to analyze, manipulate, or visualize the data. So here provided 9 ways to convert the list to a dataframe.
Method-1: Convert a list to DataFrame in Python using the pandas.DataFrame() function
The simplest and most common method to convert a list to a DataFrame in Python is to use the pandas.DataFrame() function. This method takes the list as input and converts it into a DataFrame.
import pandas as pd
# Creating the list
Country_name = ['U.S.A','Brazil','Australia','United Kingdom']
# Using the pd.dataframe()
new_result = pd.DataFrame(Country_name)
# Display the Content
print("Converted list to dataframe :",new_result)
The above code demonstrates how to convert a Python list into a Pandas DataFrame using the pandas library.
- The list named Country_name contains four country names, which are then passed to the pd.DataFrame() function to create a new DataFrame named new_result. Finally, the content of the DataFrame is displayed using the print() function.
Read: Python convert tuple to list
Method-2: Convert a list to DataFrame in Python using column names and index
If you have a list with column names and index values, you can convert it to a DataFrame in Python using the pandas.DataFrame() function.
import pandas as pd
# Creation of list
Country_name = ['U.S.A', 'United Kingdom', 'Australia']
new_result = pd.DataFrame(Country_name,index = [0,1,2], columns = ['Country_name'])
# Display the Content
print("Converted list to DataFrame :",new_result)
The above code uses pandas library to create a dataframe from a list of country names. The list of country names is stored in a variable called Country_name.
- A new dataframe is created using pd.DataFrame() method which takes the list of country names as an argument. Additionally, it has two more arguments, index and columns.
- The index argument is used to specify the row labels for the dataframe and columns argument is used to specify the column labels for the dataframe.
- Finally, the created dataframe is printed to the console using the print() statement.
Output: Converted list to DataFrame : Country_name
0 U.S.A
1 United Kingdom
2 Australia
Read: Convert string to float in Python
Method-3: Convert a list to DataFrame in Python using zip() function
This method converts a list to a DataFrame using the zip() function in Python. The zip() function takes multiple lists as input and returns an iterator that aggregates elements from each of the input lists.
# Import the Pandas library as "pd"
import pandas as pd
# Create two lists, one for car names and another for their values
Cars_in_USA = ['BMW', 'Tesla', 'Volkswagen']
new_values = [674, 723, 178]
# Combine the two lists using the "zip()" function, and create a Pandas DataFrame using "pd.DataFrame()"
new_result = pd.DataFrame(zip( Cars_in_USA, new_values), columns = ['Cars_name', 'value'])
# Print the resulting DataFrame
print(new_result)
The above code first creates two lists, one for car names and another for their values in the USA.
- The “zip()” function is then used to combine these two lists into a tuple, and the resulting tuples are used to create a Pandas DataFrame.
- The resulting DataFrame has two columns, “Cars_name” and “value”, and three rows, one for each car. Finally, the resulting DataFrame is printed to the console.
Output: Cars_name value
0 BMW 674
1 Tesla 723
2 Volkswagen 178
Read: Python convert binary to decimal
Method-4: Convert a list to DataFrame in Python using the multidimensional list
This method converts a multidimensional list to a DataFrame in Python using the pandas.DataFrame() function. A multidimensional list is a list of lists where each sublist represents a row of data.
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)
The above code imports the Pandas library and creates a list of lists containing string and integer values.
- It then uses the pd.DataFrame() function to convert the list into a Pandas DataFrame. Finally, the print() function is used to display the contents of the DataFrame.
Read: Convert tuple to string in Python
Method-5: Convert a list to DataFrame in Python using a list in the dictionary
This method first creates a dictionary with the column names as keys and the lists as values. Then pass the dictionary to the pandas.DataFrame() function to create the DataFrame.
import pandas as pd
# create a dictionary with the data
data = {'Name': ['Alex', 'Jhon', 'Rock'],
'Age': [25, 30, 35],
'Gender': ['Female', 'Male', 'Male']}
# create a DataFrame from the dictionary
df = pd.DataFrame(data)
# print the DataFrame
print(df)
The above code creates a DataFrame from a dictionary using the pandas library in Python.
- The dictionary contains three keys, ‘Name’, ‘Age’, and ‘Gender’, each having a list of corresponding values.
- The DataFrame is created by passing the dictionary to the pd.DataFrame() method. Finally, the DataFrame is printed using the print() statement.
Output:
Name Age Gender
0 Alex 25 Female
1 Jhon 30 Male
2 Rock 35 Male
Read: Python convert dictionary to an array
Method-6: Convert a list to DataFrame in Python using a multi-dimensional list with dtype and column name specified
This method converts a multi-dimensional list with column names and data types specified to a DataFrame in Python, you can use the pandas.DataFrame() function and pass in the column names and data types as parameters.
import pandas as pd
# create a multi-dimensional list with data
data = [['Alex', 25, 'Female'], ['Jhon', 30, 'Male'], ['Rock', 35, 'Male']]
# create a DataFrame from the multi-dimensional list with column names and data types specified
df = pd.DataFrame(data, columns=['Name', 'Age', 'Gender'], dtype=float)
# print the DataFrame
print(df)
The above code creates a Pandas DataFrame from a multi-dimensional list with column names and data types specified.
- The multi-dimensional list contains three lists, each containing three elements – a name, an age, and a gender. The pd.DataFrame() function is used to create the DataFrame, and the column names and data types are specified using the columns and dtype parameters.
- Finally, the DataFrame is printed to the console using the print() function.
Output:
Name Age Gender
0 Alex 25.0 Female
1 Jhon 30.0 Male
2 Rock 35.0 Male
Method-7: Convert a list to DataFrame in Python using the from_records() function
This method to convert a list to a DataFrame is to use the pandas.DataFrame.from_records() function. This method takes the list as input and converts it into a DataFrame.
import pandas as pd
# create a list
my_list = [['Jhony', 25], ['James', 30], ['Alex', 35]]
# convert the list to a DataFrame
df = pd.DataFrame.from_records(my_list, columns=['Name', 'Age'])
# print the DataFrame
print(df)
The above code imports the pandas library and creates a list called “my_list” containing sublists with the name and age information of three individuals.
- Then, it creates a pandas DataFrame using the “from_records” method, which takes the list as an argument and specifies the column names as “Name” and “Age”. Finally, it prints the DataFrame to the console.
Output: Name Age
0 Jhony 25
1 James 30
2 Alex 35
Read: How to convert a String to DateTime in Python
Method-8: Convert a list to DataFrame in Python using the from_dict() function
This method to convert a list to a DataFrame is to first convert the list to a dictionary and then use the pandas.DataFrame.from_dict() function to convert the dictionary to a DataFrame.
import pandas as pd
# create a list
my_list = [['Jhony', 25], ['James', 30], ['Alex', 35]]
# convert the list to a dictionary
my_dict = {item[0]: item[1] for item in my_list}
# convert the dictionary to a DataFrame
df = pd.DataFrame.from_dict(my_dict, orient='index', columns=['Age'])
# add a 'Name' column to the DataFrame
df['Name'] = df.index
# reset the index
df.reset_index(drop=True, inplace=True)
# print the DataFrame
print(df)
The above code first, a list of lists is created, containing names and ages of individuals.
- Then, the list is converted to a dictionary using dictionary comprehension, where names are keys and ages are values.
- The dictionary is then converted to a DataFrame using from_dict method, with the index set as the names and column name set as ‘Age’.
Output:
Age Name
0 25 Jhony
1 30 James
2 35 Alex
Method-9: Convert a list to DataFrame in Python using the list of tuples
This method creates a list of tuples with data and then creates the DataFrame using the pandas.DataFrame() function. The resulting DataFrame will have column names and data types based on the order of the tuples.
import pandas as pd
# create a list of tuples with data
data = [('USA', 332403650), ('United Kingdom', 67508936), ('Brazil', 216552394)]
# create a DataFrame from the list of tuples
df = pd.DataFrame(data, columns=['CountryName', 'Population'])
# print the DataFrame
print(df)
The above code creates a DataFrame using Pandas library from a list of tuples containing data about countries and their population.
- It first creates a list of tuples and then uses the pd.DataFrame() method to create a DataFrame with two columns ‘CountryName’ and ‘Population’. Finally, the created DataFrame is printed using the print() function.
Output:
CountryName Population
0 USA 332403650
1 United Kingdom 67508936
2 Brazil 216552394
Also, take a look at some more Python tutorials.
- How to convert dictionary to JSON in Python
- How to Convert DateTime to UNIX timestamp in Python
- How to Convert Python string to byte array
Conclusion
In this Python tutorial, we have covered how to convert a list to a dataframe in Python using the following methods:
- Using the pandas.DataFrame() function
- Using column names and index
- Using zip() function
- Using the multidimensional list
- Using a list in the dictionary
- Using a multi-dimensional list with dtype and column name specified
- Using the from_records() function
- Using the from_dict() function
- Using the list of tuples
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.