Home »
Python »
Python Programs
Create multiple dataframes in loop in Python
Learn, how can we create multiple dataframes in loop in Python?
Submitted by Pranit Sharma, on October 03, 2022
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Prerequisite
- Python Loops: Loop is functionality that runs n number of times where the value of n can be defined by the user, hence we are going to use a for loop to create DataFrames.
- Python Dictionaries: Dictionaries are used to store heterogeneous data. The data is stored in key:value pair. A dictionary is a collection that is mutable and ordered in nature and does not allow duplicates which mean there are unique keys in a dictionary. A dictionary key can have any type of data as its value, for example, a list, tuple, string, or dictionary itself.
Problem statement
Write a Python program to create multiple dataframes in loop
Create multiple dataframes in loop
To create multiple dataframes in loop, you can create a list that contains the name of different fruits, and then loop over this list, and on each traversal of the element.
Let us understand with the help of an example,
Python program to create multiple dataframes in loop
# Import pandas as pd
import pandas as pd
# Creating list
l = ['Mango','Apple','Grapes','Orange','Papaya']
# Creating a dictionary
d = {}
# Using for loop for creating dataframes
for i in l:
d[i] = pd.DataFrame()
# Display created dictionary
print("Created Dictionary:\n",d)
Output
Python Pandas Programs »