Create a Pandas DataFrame from Lists - GeeksforGeeks

02 Jun, 2021
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Creating Pandas Dataframe can be achieved in multiple ways. Let’s see how can we create a Pandas DataFrame from Lists.
YouTubeGeeksforGeeks502K subscribersCreating a Pandas DataFrame From Lists | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 8:05•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=GOBzqpdZX5A" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Code #1: Basic example
# import pandas as pdimport pandas as pd  # list of stringslst = ['Geeks', 'For', 'Geeks', 'is',             'portal', 'for', 'Geeks']  # Calling DataFrame constructor on listdf = pd.DataFrame(lst)df
Output: Code #2: Dataframe using list with index and column names
# import pandas as pdimport pandas as pd  # list of stringslst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']  # Calling DataFrame constructor on list# with indices and columns specifieddf = pd.DataFrame(lst, index =['a', 'b', 'c', 'd', 'e', 'f', 'g'],                                              columns =['Names'])df
Output: Code #3: Using zip() for zipping two lists
# import pandas as pdimport pandas as pd  # list of stringslst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']  # list of intlst2 = [11, 22, 33, 44, 55, 66, 77]  # Calling DataFrame constructor after zipping# both lists, with columns specifieddf = pd.DataFrame(list(zip(lst, lst2)),               columns =['Name', 'val'])df
Output: Code #4: Creating DataFrame using multi-dimensional list
# import pandas as pdimport pandas as pd     # List1 lst = [['tom', 25], ['krish', 30],       ['nick', 26], ['juli', 22]]    df = pd.DataFrame(lst, columns =['Name', 'Age'])df
Output: Code #5: Using multi-dimensional list with column name and dtype specified.
# import pandas as pdimport pandas as pd     # List1 lst = [['tom', 'reacher', 25], ['krish', 'pete', 30],       ['nick', 'wilson', 26], ['juli', 'williams', 22]]    df = pd.DataFrame(lst, columns =['FName', 'LName', 'Age'], dtype = float)df
Output: Code #6: Using lists in dictionary to create dataframe
# importing pandas as pd import pandas as pd   # list of name, degree, scorenme = ["aparna", "pankaj", "sudhir", "Geeku"]deg = ["MBA", "BCA", "M.Tech", "MBA"]scr = [90, 40, 80, 98]  # dictionary of lists dict = {'name': nme, 'degree': deg, 'score': scr}     df = pd.DataFrame(dict)    df 
Output:
pandas-dataframe-program
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Reading and Writing to text files in Python
*args and **kwargs in Python
sum() function in Python