Different Ways to Create Pandas Dataframe



Pandas DataFrames can be created in several ways. Let`s discuss the different ways to create a DataFrame one by one.

Method # 1: Create a Pandas DataFrame from lists of lists.

# Import pandas library

import pandas as pd

 
# initialize list of lists

data = [[[ `tom` , 10 ], [ `nick` , 15 ], [ ` juli` , 14 ]]

  
# Create a DataFrame panda

df = pd.DataFrame (data, columns = [ `Name` , ` Age` ])

 
# print data.
df

Output:

Method # 2: Creating a DataFrame from text / list

To create a DataFrame from an array / list reference, all arrays must be the same length. If an index is passed, then the length index must be equal to the length of the arrays. If no index is passed, then by default the index will be range (n), where n — array length.

# Python code demonstrates creation
# DataFrame from dict narray / lists
# Default addresses.

 

import pandas as pd

 
# initialize the list data.

data = { `Name` : [ ` Tom` , `nick` , `krish` , ` jack` ], `Age` : [ 20 , 21 , 19 , 18 ]}

 
# Create DataFrame

df = pd.DataFrame (data)

 
# Print the output.
df

Output:

Method # 3: Creates a DataFrame index using arrays.

# Python code demonstrates creating
# pandas DataFrame indexed by

  
# DataFrame using arrays.

import pandas as pd

 
# initialize the list data.

data = { ` Name` : [ `Tom` , `Jack` , ` nick` , ` juli` ], `marks` : [ 99 , 98 , 95 , 90 ]}

 
# Creates a pandas DataFrame.

df = pd.DataFrame (data, index = [ `rank1` , `rank2` , `rank3` , ` rank4` ])

 
# print data
df

Output:

Method # 4: Creating a Dataframe from a Dict List

A Pandas DataFrame can be created by passing lists of dictionaries as input. By default, dictionary keys are treated as columns.

# Python code demonstrates how to create
# Pandas DataFrame by lists of dictation.

import pandas as pd

 
# Initialize data in lists.

data = [{ ` a` : 1 , `b` : 2 , `c` : 3 }, { `a` : 10 , `b` : 20 , ` c` : 30 }]

 
# Creates a DataFrame.

df = pd.DataFrame (data)

 
# Print data
df

Output:

Another example of creating pandas DataFrame by passing lists of dictionaries and string indices.

# Python code demonstrates creation
# Pandas DataFrame, passing lists
# Dictionaries and inline pointers.

import pandas as pd

 
# Initialize list data

data = [{ `b` : 2 , `c` : 3 }, { ` a` : 10 , `b` : 20 , `c` : 30 }]

 
# Creates a padas DataFrame, passing
# Dictionary lists and line index.

df = pd.DataFrame (data, index = [ `first` , ` second` ])

 
# Print data
df

Output:

Another example of creating pandas DataFrame from lists of dictionaries with page indices ok and columns.

# Python code demonstrates creation
# Pandas DataFrame with lists
# dictionaries and
# row and column indexes.

 

import pandas as pd

 
# Initialize data lists.

data = [{ `a` : 1 , `b` : 2 } , { `a` : 5 , `b` : 10 , ` c` : 20 }]

 
# With two column indices, the values ​​are the same
# as dictionary keys

df1 = pd.DataFrame (data, index = [ `first` , `second` ], columns = [ `a` , `b` ])

 
# With two column indexes
# one index with a different name

df2 = pd.DataFrame (data, index = [ `first` , ` second` ], columns = [ `a` , `b1` ])

  
# print for the first data frame

print (df1, "" )

  
# Print for the second DataFrame.

print (df2)

Output:

Method # 5: Creating a DataFrame using the zip () function.

Two lists can be combined using the list (zip ()) function. Now create a pandas DataFrame by calling pd.DataFrame () .

# Python program to demonstrate creation
# pandas Datadaframe from lists using zip.

 

import pandas as pd 

 
# List1

Name = [ `tom` , ` krish` , `nick` , ` juli`

 
# List2

Age = [ 25 , 30 , 26 , 22

 
# get a list of tuples from two lists.
# and zip () concatenate them.

list_of_tuples = list ( zip (Name, Age)) 

  
# Assign data to tuples.
list_of_tuples 

 

 
# Convert tuple lists to
# pandas Dataframe.

df = pd.DataFrame (list_of_tuples, columns = [ ` Name` , `Age` ]) 

  
# Print data.
df 

Output:

Method # 6: Creating a DataFrame from a Dicts series.

To create a DataFrame from a Dicts series, you can pass a dictionary to form the DataFrame. The resulting index is — this is the union of all series passed into the index.

# Python code demonstrates creation
# Pandas Dataframe from the Dicts series.

 

import pandas as pd

 
# Insert data into Dicts 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 a Dataframe.

df = pd.DataFrame (d)

 
# print data.
df

Output: