How to create DataFrame from a dictionary in Python?

Create DataFrame From Dictionary

Hello there! In this tutorial, we are going to discuss the different ways to create a Pandas DataFrame object from a dictionary in Python. So, let’s get started.


Create a DataFrame from dictionary of lists

In Python, we can easily create a Pandas DataFrame object from a Python dictionary. Let’s learn the different ways to create a pandas DataFrame from a dictionary of lists one by one.

1. Using the pd.DataFrame() function

In this method, we will first create a Python dictionary of lists and pass it to the pd.DataFrame() function. Finally, the pd.DataFrame() function returns a pandas DataFrame object with the data from the dictionary of lists. Let’s implement this through Python code.

# Import pandas module
import pandas as pd 

# Create a Python dictionary of lists
data = {'Name': ['Rajan', 'Raman', 'Deepak', 'David'],
      'Roll': [11, 12, 13, 14],
      'City': ['Agra', 'Pune', 'Delhi', 'Sivan']}

# Create a pandas DataFrame dictionary of lists
# using pd.DataFrame() function
df = pd.DataFrame(data)
print(df)

Output:

Create A DataFrame Using DataFrame Function

2. Using the DataFrame.from_dict() function

Like the previous method, here also we will first create a Python dictionary of lists but pass it to the DataFrame.from_dict() function. Finally, the DataFrame.from_dict() function returns a Pandas DataFrame object with the data from the dictionary of lists. Let’s see how to implement this through Python code.

# Import pandas module
import pandas as pd 

# Create a Python dictionary of lists
data = {'Name': ['Rajan', 'Raman', 'Deepak', 'David'],
      'Roll': [11, 12, 13, 14],
      'Marks': [93, 88, 95, 75],
      'City': ['Agra', 'Pune', 'Delhi', 'Sivan']}

# Create a DataFrame from dictionary of lists
# using from_dict() function
df = pd.DataFrame.from_dict(data)
print(df)

Output:

Create A DataFrame Using DataFrame Function

Create a DataFrame from list of dictionaries

In Python, we can also create a Pandas DataFrame object from a list of dictionaries. In this method, we first create a Python list of dictionaries and pass it to the pd.DataFrame() function. Then the pd.DataFrame() function returns a Pandas DataFrame object with the data from the list of dictionaries. Let’s see how to create a DataFrame from a list of dictionaries through Python code.

# Import pandas module
import pandas as pd 

# Create a Python list of dictionaries
data = [{'Name': "Sanjay", 'Roll': 101, 'Branch': 'ECE'},
        {'Name': "Ravi", 'Roll': 102, 'Branch': 'CSE'},
        {'Name': "Thaman", 'Roll': 103, 'Branch': 'EEE'},
        {'Name': "David", 'Roll': 104, 'Branch': 'IPE'},]

# Create a DataFrame from list of dictionaries
# using pd.DataFrame() function
df = pd.DataFrame(data)
print(df)

Output:

Create A DataFrame From List Of Dictionaries

Create a DataFrame from dictionary of Python ranges

In Python, we can even create a Pandas DataFrame object from a dictionary of Python ranges. In this method, we first create a dictionary of ranges using the range() function in Python and pass it to the pd.DataFrame() function. Then the pd.DataFrame() function returns a Pandas DataFrame object with the data from the dictionary of ranges. Let’s see how to create a DataFrame from a dictionary of Python ranges through Python code.

# Import pandas module
import pandas as pd 

# Create a dictionary of
# Python ranges
data = {'Table-1': range(1, 11, 1),
        'Table-2': range(2, 21, 2),
        'Table-3': range(3, 31, 3),
        'Table-4': range(4, 41, 4),
        'Table-5': range(5, 51, 5)}

# Create a DataFrame from a dictionary
# of Python ranges
# using pd.DataFrame() function
df = pd.DataFrame(data)
print(df)

Output:

Create A DataFrame From Dictionary Of Python Ranges

Create a DataFrame from dictionary with user-defined indexes

In Python, we can create a Pandas DataFrame object from a dictionary with user-defined indexes. In this method, we first create a Python dictionary and pass it to the pd.DataFrame() function along with the index list. Then the pd.DataFrame() function returns a Pandas DataFrame object with the data from the dictionary and index from the passed index list. Let’s see how to implement it through Python code.

# Import pandas module
import pandas as pd 

# Create a Python dictionary of lists
data = {'Name': ['Rajan', 'Raman', 'Deepak', 'David'],
      'Roll': [11, 12, 13, 14],
      'Marks': [93, 88, 95, 75],
      'City': ['Agra', 'Pune', 'Delhi', 'Sivan']}

# Create a DataFrame from dictionary of lists
# using pd.DataFrame() function with user-defined indexes
df = pd.DataFrame(data, index = ['S1', 'S2', 'S3', 'S4'])
print(df)

Output:

Create A DataFrame From Dictionary With User Defined Indexes

Conclusion

In this tutorial, we have learned the different ways to create a Pandas DataFrame object from a Python dictionary. Hope you have understood the things discussed above and ready to explore & learn more about pandas DataFrame objects. Stay tuned with us!