Different ways to create Pandas Dataframe - GeeksforGeeks

24 Dec, 2021
Pandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used pandas object. Pandas DataFrame can be created in multiple ways. Let’s discuss different ways to create a DataFrame one by one.Method #1: Creating Pandas DataFrame from lists of lists. 
Python3
# Import pandas libraryimport pandas as pd # initialize list of listsdata = [['tom', 10], ['nick', 15], ['juli', 14]] # Create the pandas DataFramedf = pd.DataFrame(data, columns = ['Name', 'Age']) # print dataframe.df
Output:  
 
YouTubeGeeksforGeeks502K subscribersDifferent Ways to Create a Pandas DataFrame | 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 / 12:54•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=dEHJmn6p39M" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Method #2: Creating DataFrame from dict of narray/listsTo create DataFrame from dict of narray/list, all the narray must be of same length. If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will be range(n) where n is the array length. 
Python3
# Python code demonstrate creating# DataFrame from dict narray / lists# By default addresses. import pandas as pd # initialize data of lists.data = {'Name':['Tom', 'nick', 'krish', 'jack'],        'Age':[20, 21, 19, 18]} # Create DataFramedf = pd.DataFrame(data) # Print the output.df
Output:  
  Method #3: Creates a indexes DataFrame using arrays. 
Python3
# Python code demonstrate creating# pandas DataFrame with indexed by # DataFrame using arrays.import pandas as pd # initialize data of lists.data = {'Name':['Tom', 'Jack', 'nick', 'juli'],        'marks':[99, 98, 95, 90]} # Creates pandas DataFrame.df = pd.DataFrame(data, index =['rank1',                                'rank2',                                'rank3',                                'rank4']) # print the datadf
Output:  
  Method #4: Creating Dataframe from list of dictsPandas DataFrame can be created by passing lists of dictionaries as a input data. By default dictionary keys taken as columns. 
Python3
# Python code demonstrate how to create# Pandas DataFrame by lists of dicts.import pandas as pd # Initialize data to lists.data = [{'a': 1, 'b': 2, 'c':3},        {'a':10, 'b': 20, 'c': 30}] # Creates DataFrame.df = pd.DataFrame(data) # Print the datadf
Output:  
Another example to create pandas DataFrame by passing lists of dictionaries and row indexes. 
Python3
# Python code demonstrate to create# Pandas DataFrame by passing lists of# Dictionaries and row indices.import pandas as pd # Initialize data of listsdata = [{'b': 2, 'c':3}, {'a': 10, 'b': 20, 'c': 30}] # Creates pandas DataFrame by passing# Lists of dictionaries and row index.df = pd.DataFrame(data, index =['first', 'second']) # Print the datadf
Output:  
Another example to create pandas DataFrame from lists of dictionaries with both row index as well as column index. 
Python3
# Python code demonstrate to create a# Pandas DataFrame with lists of# dictionaries as well as# row and column indexes.  import pandas as pd  # Initialize lists data.data = [{'a': 1, 'b': 2},        {'a': 5, 'b': 10, 'c': 20}]  # With two column indices, values same# as dictionary keysdf1 = pd.DataFrame(data, index =['first',                                 'second'],                   columns =['a', 'b'])  # With two column indices with# one index with other namedf2 = pd.DataFrame(data, index =['first',                                 'second'],                   columns =['a', 'b1'])  # print for first data frameprint (df1, "\n")  # Print for second DataFrame.print (df2)
Output:  
  Method #5: Creating DataFrame using zip() function.Two lists can be merged by using list(zip()) function. Now, create the pandas DataFrame by calling pd.DataFrame() function. 
Python3
# Python program to demonstrate creating# pandas Datadaframe from lists using zip.   import pandas as pd   # List1Name = ['tom', 'krish', 'nick', 'juli']   # List2Age = [25, 30, 26, 22]   # get the list of tuples from two lists.# and merge them by using zip().list_of_tuples = list(zip(Name, Age))   # Assign data to tuples.list_of_tuples   # Converting lists of tuples into# pandas Dataframe.df = pd.DataFrame(list_of_tuples,                  columns = ['Name', 'Age'])    # Print data.df
Output:  
 Method #6: Creating DataFrame from Dicts of series.To create DataFrame from Dicts of series, dictionary can be passed to form a DataFrame. The resultant index is the union of all the series of passed indexed. 
Python3
# Python code demonstrate creating# Pandas Dataframe from Dicts of series. import pandas as pd # Initialize data to Dicts of series.d = {'one' : pd.Series([10, 20, 30, 40],                       index =['a', 'b', 'c', 'd']),      'two' : pd.Series([10, 20, 30, 40],                        index =['a', 'b', 'c', 'd'])} # creates Dataframe.df = pd.DataFrame(d) # print the data.df
Output:  
 
anikaseth98
rajeev0719singh
chhabradhanvi
simranarora5sos
Picked
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Create a Pandas DataFrame from Lists
Reading and Writing to text files in Python
*args and **kwargs in Python
sum() function in Python
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?