Can we convert list to dataframe in python?
At times, you may need to convert a list to Pandas DataFrame in Python. Show
You may then use this template to convert your list to a DataFrame: import pandas as pd list_name = ['item_1', 'item_2', 'item_3',...] df = pd.DataFrame (list_name, columns = ['column_name']) In the next section, you’ll see how to perform the conversion in practice. Examples of Converting a List to Pandas DataFrameExample 1: Convert a ListLet’s say that you have the following list that contains 5 products: products_list = ['laptop', 'printer', 'tablet', 'desk', 'chair'] You can then apply the following syntax in order to convert the list of products to Pandas DataFrame: import pandas as pd products_list = ['laptop', 'printer', 'tablet', 'desk', 'chair'] df = pd.DataFrame (products_list, columns = ['product_name']) print (df) This is the DataFrame that you’ll get:
Example 2: Convert a List of ListsHow would you then convert a list of lists to a DataFrame? For instance, let’s say that you have the following list of lists: products_list = [['laptop',1300],['printer',150],['tablet',300],['desk',450],['chair',200]] You can then run the code below to perform the conversion to a DataFrame: import pandas as pd products_list = [['laptop',1300],['printer',150],['tablet',300],['desk',450],['chair',200]] df = pd.DataFrame (products_list, columns = ['product_name', 'price']) print (df) And this is the result that you’ll get:
Alternatively, you may have your list of lists as follows: products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]] Therefore, the Python code to perform the conversion to a DataFrame would be: import pandas as pd products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]] df = pd.DataFrame (products_list).transpose() df.columns = ['product_name', 'price'] print (df) Run the code, and you’ll get the same DataFrame:
Check the Object TypeIf needed, you may also check the type of the objects (e.g., List vs. DataFrame) by applying this code: import pandas as pd products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]] df = pd.DataFrame (products_list).transpose() df.columns = ['product_name', 'price'] print ('products_list: ' + str(type(products_list))) print ('df: ' + str(type(df))) And here is the result:
Applying Stats Using Pandas (optional)Once you converted your list into a DataFrame, you’ll be able to perform an assortment of operations and calculations using Pandas. For instance, you may use Pandas to derive some statistics about your data. In the context of our example, you can apply the code below in order to get the mean, max and min price using Pandas: import pandas as pd products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]] df = pd.DataFrame (products_list).transpose() df.columns = ['product_name', 'price'] mean_value = df['price'].mean() max_value = df['price'].max() min_value = df['price'].min() print ('The mean price is: ' + str(mean_value)) print ('The max price is: ' + str(max_value)) print ('The min price is: ' + str(min_value)) Run the Python code, and you’ll get these stats:
An Opposite ScenarioSometimes, you may face an opposite situation, where you’ll need to convert a DataFrame to a list. If that’s the case, you may want to check the following guide that explains the steps to perform the conversion. You can directly call the
Output:
And if you have multiple lists and you want to make a dataframe out of it. You can do it as following:
Output:
How do I convert a list to a DataFrame in Python?Use pandas. DataFrame() constructor to convert a list to a DataFrame. Use pandas. DataFrame(data, columns) to convert a list to a DataFrame.
Can we create DataFrame from list?The pandas DataFrame can be created by using the list of lists, to do this we need to pass a python list of lists as a parameter to the pandas. DataFrame() function. Pandas DataFrame will represent the data in a tabular format, like rows and columns.
Can you put a list in a DataFrame Python?You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame.
How do I turn a list into a DataFrame row?Method 1: Using T function
This is known as the Transpose function, this will convert the list into a row. Here each value is stored in one column. Example: Python3.
|