I think the best is create dict of objects - see How do I create a variable number of variables?

You can use dict of DataFrames by converting groupby object to dict:

d = dict(tuple(df.groupby('month')))
print (d)
{1:    month dest
0      1    a
1      1   bb, 2:    month dest
2      2   cc
3      2   dd, 3:    month dest
4      3   ee, 4:    month dest
5      4   bb}

print (d[1])
   month dest
0      1    a
1      1   bb

Another solution:

for i, x in df.groupby('month'):
    globals()['dataframe' + str(i)] = x

print (dataframe1)
   month dest
0      1    a
1      1   bb
Answer from jezrael on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ dynamically assigning name of dataframe in a loop. stuck!
r/learnpython on Reddit: Dynamically assigning name of dataframe in a loop. Stuck!
February 2, 2018 -

Going pseudo-code this out, perhaps somebody has encountered this sort of issue before. Have not had luck reading through stackoverflow posts.

I have a list of months and a df for each month with data that includes delivery volume and a time. These named like 'df_1701_unfiltered'.

I previously hardcoded my query logic, but on mobile now. That's not what I'm worried about so please disregard the pseudo aspect (I'm on mobile atm).

I want to create a new, separate dataframe for each month that is a filtered version of the original. Here is my thought process.

months = ['1701', '1702', '1703']

For month in month: "df_"+month+"filtered" = "df"+month+"_unfiltered".query("time > start and time < end")

I'm able to do something similar within a single dataframe using .apply to create dynamic columns. It throws an "cannot assign to operator" error each time.

Any idea how I can do this for entire dataframes?

Discussions

python - Create new dataframe in pandas with dynamic names also add new column - Stack Overflow
1 Dataframe is not defined when trying to concatenate in loop (Python - Pandas) 0 Can I build a loop function that automatically creates multiple data-frames at one go? 0 How to create variables and read several excel files in a loop with pandas? 0 Changing variable names and creating new ones dynamically... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to dynamically name a dataframe within this for loop - Stack Overflow
I want to be able to dynamically rename the results dataframe based on which dataset I am using. My code looks like this (where 'feature_cols' are the names of the chemicals): count=0 dataframe=[] #loop through the three datasets (In reality I have many more than three) for dataset in [first, ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Dynamic dataframe names creation - Stack Overflow
Hi I need my dataframe with different names. The dataframe is creating insde a for loop as shown below. for lambda_ in range(0,len(tuned_parameter)-1): print ('................................... More on stackoverflow.com
๐ŸŒ stackoverflow.com
trying to dynamically name and reference a dataframe, getting error 'SyntaxError: can't assign to function call'
General rule of thumb - if you need dynamically created variable names - you are doing something wrong. Even though it is possible, doing so would be an awfully bad idea, ie how will you then reference these variables later in your code? More on reddit.com
๐ŸŒ r/learnpython
4
1
May 5, 2017
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-21151.html
dynamically create variables' names in python
May 14, 2021 - Hi guys, i want to create variables in the following way: assign a name (e.g. var1), then add the name to the prefix of the variable: name = 'var_1' this_is_+name = pd.DataFrame()the outcome i would l
Top answer
1 of 2
24

Creating variables with dynamic names is typically a bad practice.

I think the best solution for your problem is to store your dataframes into a dictionary and dynamically generate the name of the key to access each dataframe.

import copy

dict_of_df = {}
for ym in [201511, 201612, 201710]:

    key_name = 'df_new_'+str(ym)    

    dict_of_df[key_name] = copy.deepcopy(df)

    to_change = df['YearMonth']< ym
    dict_of_df[key_name].loc[to_change, 'new_col'] = ym   

dict_of_df.keys()
Out[36]: ['df_new_201710', 'df_new_201612', 'df_new_201511']

dict_of_df
Out[37]: 
{'df_new_201511':     A    B  ID                       t  YearMonth  new_col
 0  -a    a   1 2016-12-05 07:53:35.943     201612   201612
 1   1  NaN   2 2016-12-05 07:53:35.943     201612   201612
 2   a    c   2 2016-12-05 07:53:35.943     201612   201612,
 'df_new_201612':     A    B  ID                       t  YearMonth  new_col
 0  -a    a   1 2016-12-05 07:53:35.943     201612   201612
 1   1  NaN   2 2016-12-05 07:53:35.943     201612   201612
 2   a    c   2 2016-12-05 07:53:35.943     201612   201612,
 'df_new_201710':     A    B  ID                       t  YearMonth  new_col
 0  -a    a   1 2016-12-05 07:53:35.943     201612   201710
 1   1  NaN   2 2016-12-05 07:53:35.943     201612   201710
 2   a    c   2 2016-12-05 07:53:35.943     201612   201710}

 # Extract a single dataframe
 df_2015 = dict_of_df['df_new_201511']
2 of 2
1

There is a more easy way to accomplish this using exec method. The following steps can be done to create a dataframe at runtime.

1.Create the source dataframe with some random values.

import numpy as np
import pandas as pd
    
df = pd.DataFrame({'A':['-a',1,'a'], 
                   'B':['a',np.nan,'c'],
                   'ID':[1,2,2]})

2.Assign a variable that holds the new dataframe name. You can even send this value as a parameter or loop it dynamically.

new_df_name = 'df_201612'

3.Create dataframe dynamically using exec method to copy data from source dataframe to the new dataframe dynamically and in the next line assign a value to new column.

exec(f'{new_df_name} = df.copy()')
exec(f'{new_df_name}["new_col"] = 123') 

4.Now the dataframe df_201612 will be available on the memory and you can execute print statement along with eval to verify this.

print(eval(new_df_name))
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 54990451 โ€บ dynamic-dataframe-names-creation
python - Dynamic dataframe names creation - Stack Overflow
whole_dataframes = {} #k = int(np.floor(float(X.shape[0]) / number_folds)) weights = np.zeros((3,num_portfolios)) for lambda_ in range(0,len(tuned_parameter)-1): print ('....................................',lambda_) i=0 appended_data = [] for train_index, test_index in kf.split(X): print("Train:", train_index, "Validation:",test_index) X_train, X_test = X.iloc[train_index], X.iloc[test_index] print ('X train ............',X_train.shape) print ('X_test...............',X_test.shape) mean_returns_Train = X_train.mean() cov_matrix_Train=X_train.cov() mean_returns_Test = X_test.mean() cov_matrix_T
Find elsewhere
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ create-multiple-dataframes-in-loop.aspx
Create multiple dataframes in loop in Python
October 3, 2022 - Write a Python program to 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.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 67153826 โ€บ python-dynamic-dataframe-name
pandas - Python dynamic dataframe name - Stack Overflow
So at the end you have result as [{"aaa.csv": df},{"bbb.csv": df},] . If your file_name are not unique then need to find way to create key. ... Try this one. credits to this cool video: https://www.youtube.com/watch?v=eMOA1pPVUc4 ยท import os import pandas as pd path = "./path" files = [file for file in os.listdir(path) if not file.startswith('.')] # Ignore hidden files #Define the next table as Pandas Dataframe all_months_data = pd.DataFrame() #loop to check each file by file name e append to the previous for file in files: current_data = pd.read_csv(path+"/"+file) all_months_data = pd.concat([all_months_data, current_data]) #create a CSV DF with all data all_months_data.to_csv("all_data_copy.csv", index=False)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ create-a-column-using-for-loop-in-pandas-dataframe
Create a column using for loop in Pandas Dataframe - GeeksforGeeks
June 9, 2022 - Converting lists to DataFrames is crucial in data analysis, Pandas enabling you to perform sophisticated data manipulations and analyses with ease. List to Dataframe Example# Simple listdata = [1, 2, 3, 4, 5]# Convert to DataFramedf = pd.DataFrame(data, columns=['Numbers'])Here we will discuss diffe ... Pandas is basically the library in Python used for Data Analysis and Manipulation.
๐ŸŒ
KDnuggets
kdnuggets.com โ€บ 2022 โ€บ 08 โ€บ customize-data-frame-column-names-python.html
Customize Your Data Frame Column Names in Python - KDnuggets
After constructing the dictionary columnnames with the original and new column names we will then passing the dictionary to the rename method ... columnnames = {} count = 0 for i in df.columns: count += 1 columnnames[i] = f"WEEK_{count}_ATTENDANCE" columnnames ... We would then be using for loop to iterate over all the columns of the Data Frame, where in every iteration the first occurrence of the underscore will be replaced by no space.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 43796074 โ€บ trying-to-dynamically-name-and-later-reference-dataframe-name-as-a-variable-get
python - trying to dynamically name and later reference dataframe name as a variable, getting error 'SyntaxError: can't assign to function call' - Stack Overflow
#define list of fields to run match for fieldlist = ['MATTER NUMBER','MATTER NAME','CLAIM NUMBER LISTING'] #loop through each field in fieldlist for field in fieldlist: #define dfname as the field with spaces replaced with underscores dfname = '{}'.format(field.replace(' ','_')) #create df with dfname '{}'.format(dfname) = checkdf['{}'.format(field)].dropna() ... edit: apologies if this was confusing. I'm attempting to create dataframe names and columns associated with each field in fieldlist from checkdf.