how to convert lists and dictionaries to dataframe in python

How To Convert Lists or Dictionaries to DataFrame (code snippets)

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
0apple
1banana
2grapes
3orange

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
1apple
2banana
3grapes
4orange

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

 01
0applefruit 1
1bananafruit 2
2grapesfruit 3
3orangefruit 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

 0123
0applebananagrapesorange
1fruit 1fruit 2fruit 3fruit 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

 01
0applefruit 1
1bananafruit 2
2grapesfruit 3
3orangefruit 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 NamePrice Per Unit
0apple2
1banana5
2grapes1
3orange7

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 NameCapital
0The NetherlandsAmsterdam
1DenmarkCopenhagen
2GermanyBerlin

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

NameAgeHeight
0Jones12152
1Ally14153
2Michael15160
3Silas11150
4Ritika13170

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

NamesMarks in MathematicsMarks in Science
0Ali5072
1Ajeet5273
2Jones6047
3Rob7252
4Shahrukh1047

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

NamesMarks in MathematicsMarks in Science
0Ali5072
1Ajeet5273
2Jones6047
3Rob7252
4Shahrukh1047

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.

Scroll to Top