How to Create a List of Dataframe Using Loop in Python

To create a list of dataframes using a loop in Python, you can use a for loop along with the append() function.

The following example shows how to create a list of dataframes using a loop in Python.

Using for Loop & append() Function

We can use a for loop and the append() function to create a list of dataframes.

Suppose we have the following data:

# Import pandas library
import pandas as pd

# Declare data
data = [
    {'Product_Name': 'Laptop', 'Price': 4500, 'Status': 1},
    {'Product_Name': 'Mobile', 'Price': 550, 'Status': 1},
    {'Product_Name': 'Printer', 'Price': 250, 'Status': 0},
    {'Product_Name': 'Keyboard', 'Price': 50, 'Status': 1}
]

# Declare empty list
list_of_dfs = []

for i in data:
    df = pd.DataFrame(i, index=[i['Product_Name']])
    list_of_dfs.append(df)

# Display the list of dataframes
for df in list_of_dfs:
    print(df)

Output: 👇️

            Product_Name  Price  Status
Laptop            Laptop   4500       1
         Product_Name  Price  Status
Mobile        Mobile    550       1
         Product_Name  Price  Status
Printer       Printer    250       0
          Product_Name  Price  Status
Keyboard     Keyboard     50       1

In this example, we use a for loop to iterate over the list of dictionaries data. For each dictionary, we create a dataframe df and append it to the list list_of_dfs.

The output shows the list of dataframes created using the loop.

Conclusion

We can use a for loop and the append() function to create a list of dataframes in Python.