Creating New Dataframes in Loop in Python

Creating and modifying dataframes with a for loop python

The second method should be used in python. As the definition is in a clear place. Further the variables are scoped as much as possible. You can easily look for the dataframes which were defined by looking for the keys of the dictionary.

You can add a new column by indexing:

d = {}
for i in tablelist1:
d[i] = pd.DataFrame()
d[i]['Value'] = df1["profit"] - df1["tax"]

The first method creates global variables with the name given by the table array. Therefore if you would like to append new columns. You would not use table as the variable name of the dataframe, but the name given from the strings. Something again with exec. This way I do not recommend as variables are defined as globals, and the way they are defined is very hidden. See following:

import pandas as pd

tablelist = ['QTD','YTD','OneYear','Inception']

for table in tablelist:
exec('{} = pd.DataFrame()'.format(table))
exec(table + "['col1'] = []")

print(QTD) # or others

create multiple new data frames in a loop by subsetting another and adding a suffix

Your buckets_list is simply a list of strings and not of dataframes. And if you really want to keep the names of the variables and add a prefix, have buckets_list be a dictionary in the following format: {dataframe_variable_name: dataframe_object, and iterate through it by using for key, val in buckets_list.items():, where the key will be the variable name and val will be the dataframe object.

EDIT: for clarification and answering the OP's comment

buckets_list = {dataframe1_name: dataframe1,
dataframe2_name: dataframe2,

Just fill it up with all the dataframes you want...

Create a dataframe using while loop in pandas

Pandas dataframes are normally used for manipulating existing data through unified operations on full columns/rows, not iterating over things, but you can accomplish the effect you're looking for by just generating the list normally and passing it into dataframe:

i = s['c'][1]
l = []
while True:
i += 2
if i >= s['b'][1]:


How to create a dataframe with every iteration of a for loop

Use globals() to create your variables dynamically. Use with caution, it's not really a good practice but it should work:

for i in range(100):
get_data = [data for data in cur]
df = pd.DataFrame(get_data)
globals()[f"df_{i}"] = df # <- replace your dict by globals()

Related Topics

Leave a reply