Create List of dataframes For Loop Python
To create a list of DataFrames in Python, you can use the pandas library. Here’s an example of how you can create a list of DataFrames and then loop through them:
import pandas as pd# Create an empty list to store the DataFrames
dataframes_list = []# Create DataFrame 1
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
dataframes_list.append(df1)# Create DataFrame 2
df2 = pd.DataFrame({'C': ['a', 'b', 'c'], 'D': ['d', 'e', 'f']})
dataframes_list.append(df2)# Loop through the list of DataFrames
for df in dataframes_list:
# Perform operations on each DataFrame 'df'
print(df)
# Or apply any other desired operations
# ...
In this example, we create an empty list called dataframes_list to store the DataFrames. Then, we create two DataFrames (df1 and df2) using the pd.DataFrame() constructor, and we append them to the dataframes_list using the append() method.
Get Zeeshan Ali’s stories in your inbox
Join Medium for free to get updates from this writer.
After that, we use a for loop to iterate through each DataFrame in the dataframes_list and perform operations on them. In this case, we are simply printing each DataFrame, but you can apply any other operations or analysis that you need.






