We hope you love the products we recommend! Just so you know, when you buy through links on our site, we may earn an affiliate commission. This adds no cost to our readers, for more information read our earnings disclosure.
This is a comprehensive approach to convert lists, lists of lists, or dictionaries of lists to DataFrame using Pandas Library in Python 3 (with code and examples).
Python’s fantastic ecosystem of data-centric python packages makes it an excellent language for conducting data analysis.
One such tool, Pandas, dramatically simplifies the process of importing and analyzing data.
You will discover more about lists and data frames in this session.
Additionally, we covered various list to Python data frame conversion techniques.
The most important data structure in Python is the list.
The list’s components are not required to be of the same data type, and all string operations are equally applicable to the various list data types.
Syntax
Use Python’s pd.DataFrame()
method to convert a list to a DataFrame. An internal library function called pandas DataFrame() accepts a list as a parameter and creates a DataFrame from it.
pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
Here data is a list or list of lists we want to convert.
The index can be changed according to requirement, we will see it in examples, columns representing the name of columns, we can also change the data types of elements by using dtypes and the default value of None operates like “copy = True” for dict data.
The default of None for DataFrame or 2D ndarray input operates as “copy = False”
1. Convert one list into a single column DataFrame
In this example, we will convert a list into a single column DataFrame.
Input
# import pandas library
import pandas as pd
# take a list
lst = [' apple ' , ' banana ' , ' grapes ' , ' orange ']
df = pd.DataFrame(lst)
# display df
print(df)
Output
0 | |
0 | apple |
1 | banana |
2 | grapes |
3 | orange |
2. Convert lists to DataFrame with a customized index
In this example we will see, how can we change the format of index.
Input
# import pandas library
import pandas as pd
# take a list
lst = [' apple ' , ' banana ' , ' grapes ' , ' orange ']
df = pd.DataFrame(lst, index=[1,2,3,4])
#display df
print(df)
Output
0 | |
1 | apple |
2 | banana |
3 | grapes |
4 | orange |
See we have changed the index range; you can also name the index through list.
3. Converting multiple lists to DataFrame
If we have multiple lists, then we convert them into DataFrame by two methods.
Method A: Use transpose() method to convert multiple lists to df
Input
# import pandas library
import pandas as pd
# take a list
lst=[' apple ' , ' banana ' , ' grapes ' , ' orange ']
name = [' fruit 1 ' , ' fruit 2 ', ' fruit 3 ' , ' fruit 4 ' ]
df = pd.DataFrame([lst, name]).transpose()
# display df
print(df)
Output
0 | 1 | |
0 | apple | fruit 1 |
1 | banana | fruit 2 |
2 | grapes | fruit 3 |
3 | orange | fruit 4 |
If we didn’t use the transpose function, DataFrame takes every list as a single row.
# import pandas library
import pandas as pd
# take a list
lst=[' apple ' , ' banana ' , ' grapes ' , ' orange ']
name = [' fruit 1 ' , ' fruit 2 ', ' fruit 3 ' , ' fruit 4 ' ]
df = pd.DataFrame([lst, name])
# display df
print(df)
Output
0 | 1 | 2 | 3 | |
0 | apple | banana | grapes | orange |
1 | fruit 1 | fruit 2 | fruit 3 | fruit 4 |
Method B: Use zip() method to convert multiple lists to DataFrame
Input
# import pandas library
import pandas as pd
# take a list
lst=[' apple ' , ' banana ' , ' grapes ' , ' orange ']
name = [' fruit 1 ' , ' fruit 2 ', ' fruit 3 ' , ' fruit 4 ' ]
df = pd.DataFrame(zip(lst,name))
# display df
print(df)
Output
0 | 1 | |
0 | apple | fruit 1 |
1 | banana | fruit 2 |
2 | grapes | fruit 3 |
3 | orange | fruit 4 |
4. Converting lists to DataFrame by customized columns names
In this example, we see how can we change the name of columns.
Input
# import pandas library
import pandas as pd
# take a list
lst=[' apple ' , ' banana ' , ' grapes ' , ' orange ']
price = [ 2 , 5 , 1 , 7]
df = pd.DataFrame(zip(lst,price),columns=[ ' Fruit Name ' , ' Price Per Unit '])
# display df
print(df)
Output
Fruit Name | Price Per Unit | |
0 | apple | 2 |
1 | banana | 5 |
2 | grapes | 1 |
3 | orange | 7 |
5. Creating DataFrame from a list of lists
In this section, we will learn how can we convert a list of lists to DataFrame
Method A: Use the pd.dataframe() method
Input
# import pandas library
import pandas as pd
# take a list
lst=[[' The Netherlands ' , ' Amsterdam '] , [ ' Denmark ' , ' Copenhagen '] , [ ' Germany ' , ' Berlin ']]
df = pd.DataFrame(lst,columns=[ ' Country Name ' , ' Capital '])
# display df
print(df)
Output
Country Name | Capital | |
0 | The Netherlands | Amsterdam |
1 | Denmark | Copenhagen |
2 | Germany | Berlin |
Method B: Use the transpose() function
Other examples to convert a list of lists or a multi-dimensional list to a DataFrame using the transpose() function:
Input
#import pandas library
import pandas as pd
# take a list
lst=[[' Jones ' , ' Ally ' , ' Michael ' , ' Silas ' , ' Ritika '] , [12 , 14 , 15 , 11 , 13] , [ 152 , 153 , 160 , 150 , 170]]
df = pd.DataFrame(lst).transpose()
df.columns=[' Name ', ' Age ' , ' Height ']
# display df
print(df)
Output
Name | Age | Height | |
0 | Jones | 12 | 152 |
1 | Ally | 14 | 153 |
2 | Michael | 15 | 160 |
3 | Silas | 11 | 150 |
4 | Ritika | 13 | 170 |
6. Converting dictionary of lists to DataFrame
In this example, you will see how you can convert multi-lists to a dictionary and then into DataFrame.
Input
# import pandas library
import pandas as pd
# take a list
name = [ ' Ali ' , ' Ajeet ' , ' Jones ' , ' Rob ' , ' Shahrukh ']
math_marks = [ 50 , 52 , 60 , 72 , 10]
science_marks = [ 72 , 73 , 47 , 52 , 47]
subject = [ ' Mathematics ' , ' Science ']
dic={ " Names " : name , " Marks in Mathematics " : math_marks , " Marks in Science " : science_marks}
df = pd.DataFrame(dic)
# display df
print(df)
Output
Names | Marks in Mathematics | Marks in Science | |
0 | Ali | 50 | 72 |
1 | Ajeet | 52 | 73 |
2 | Jones | 60 | 47 |
3 | Rob | 72 | 52 |
4 | Shahrukh | 10 | 47 |
But if we do this without creating a dictionary, we have to define column names as well.
Input
# import pandas library
>>import pandas as pd
# take a list
name = [ ' Ali ' , ' Ajeet ' , ' Jones ' , ' Rob ' , ' Shahrukh ']
math_marks = [ 50 , 52 , 60 , 72 , 10]
science_marks = [ 72 , 73 , 47 , 52 , 47]
subject = [ ' Mathematics ' , ' Science ']
df = pd.DataFrame([name , math_marks , science_marks]).transpose()
df.columns=[' Name ', ' Marks in Mathematics ' , ' Marks in Science ']
# display df
print(df)
Output
Names | Marks in Mathematics | Marks in Science | |
0 | Ali | 50 | 72 |
1 | Ajeet | 52 | 73 |
2 | Jones | 60 | 47 |
3 | Rob | 72 | 52 |
4 | Shahrukh | 10 | 47 |
See both of the methods gives the same results. So, we see if we use a dictionary, it reduces our costs.
Conclusion:
In this article, you saw how you can convert different lists to DataFrame with the help of different techniques. In the end, we concluded that if we use a dictionary of lists, it is more efficient than all the other methods.