Create pandas dataframe from lists using dictionary

Creating pandas data-frame from lists using dictionary can be achieved in multiple ways.

Method #1: Using pandas.DataFrame

With this method in Pandas we can transform a dictionary of list to a dataframe.



filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
        'degree': ["MBA", "BCA", "M.Tech", "MBA"],
        'score':[90, 40, 80, 98]}
  
df = pd.DataFrame(dict)
  
df

chevron_right


Output:

 

Method #2: Using from_dict() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
        'degree': ["MBA", "BCA", "M.Tech", "MBA"],
        'score':[90, 40, 80, 98]}
  
df = pd.DataFrame.from_dict(dict)
  
df

chevron_right


Output:

With the use of this function, we get some flexibility in arranging our data, such as the orientation of data, data type and name of the columns can be entered as parameter in the function.



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.