This isn't a pythonic thing to do, have you thought about instead creating a list of dataframes?

df=pd.DataFrame.copy(mef_list)
form=['','_M3','_M6','_M9','_M12','_LN','_C']
list_of_df = list()
for i in range(0, len(form)):
    df=pd.DataFrame.copy(mef_list)
    df['Variable_new']=df['Variable']+str(form[i])
    list_of_df.append(df)

Then you can access 'df0' as list_of_df[0]

You also don't need to iterate through a range, you can just loop through the form list itself:

form=['','_M3','_M6','_M9','_M12','_LN','_C']
list_of_df = list()
for i in form:
    df=pd.DataFrame.copy(mef_list)
    df['Variable_new']=df['Variable']+str(i) ## You can remove str() if everything in form is already a string
    list_of_df.append(df)
Answer from TheHCA on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how to change variable name in loop
r/learnpython on Reddit: How to change variable name in loop
December 29, 2021 -

I have a large data frame I am creating smaller data frames with. Basically, there is one giant data frame with different departments information, so, I am creating smaller data frames, all the rows labeled purchasing will go into a smaller data frame, engineering, accounting, etc.

I created a while loop to go through the length of the large data frame and want to assign each smaller data frame with it's own name. Code might explain it better. It is below.

n=0

while n < len(df_dept_list.index):

dept = df_dept_list.iloc[n]

df_dept = df_all_data [
        (df_all_data['Department'] == dept)].dropna()

 n = n + 1

I would like to just have the data frame name change each iteration like df_dept0, then df_dept1, something that changes with the value 'n'.

Any ideas how to?

🌐
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?

🌐
Databricks Community
community.databricks.com › t5 › data-engineering › python-generate-new-dfs-from-a-list-of-dataframes-using-for-loop › td-p › 21650
Python: Generate new dfs from a list of dataframes using for loop
December 2, 2022 - corrs_b2b_fast = (df2_b2b_fast[['adv', 'id']] .groupby('id') .corrwith(df1['pkg_yld']) .rename(columns={'adv' : 'correl'}) .reset_index()) corrs_b2c_fast = (df2_b2c_fast[['adv', 'id']] .groupby('id') .corrwith(df1['pkg_yld']) .rename(columns={'adv' : 'correl'}) .reset_index()) ... Question: I want to consolidate the steps where (1) I extract my 2 dataframes, (2) estimate the correlations and (3) convert to 2 dataframes using a for loop (corrs_b2b_fast, corrs_b2c_fast); I started below but got stuck:
🌐
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 › 58872310 › rename-and-create-a-dataframe-inside-a-for-loop
python - Rename and create a dataframe inside a For loop? - Stack Overflow
You are not using the loop variable month in the loop. I believe you want to create multiple dataframes, one for each month. To do this, you could use a dictionary: import pandas as pd df_dict = {} name = ['jan','feb'] for month in name: df_dict[month] = pd.DataFrame([month]) for month in name: print("key: ", month) print("dataframe:") print(df_dict[month], end='\n\n')
🌐
Reddit
reddit.com › r/learnpython › assigning column names through for loop in pandas
r/learnpython on Reddit: Assigning column names through for loop in Pandas
October 8, 2017 -

Hello fellow strangers! I am trying to name pandas dataframe columns based on different years, from 2015 to 2025. I could do this manually like this:

import pandas as pd
a = pd.DataFrame
a['2015'] = "2015"
a['2016'] = "2016"
a['2017'] = "2017"

But I thought I could make it work with string formatting and a for loop:

import pandas as pd
years = 10
starting_year = 2015

for each in range(years):
    a = pd.DataFrame
    a['%s' % (starting_year + each + 1)] = starting_year+each+1

But this throws me the error: TypeError: 'type' object does not support item assignment

I remember once reading that dictionaries can be used dynamically and something tells me I should use them for this specific problem but no insights yet... Anyone help??

🌐
CopyProgramming
copyprogramming.com › howto › rename-and-create-a-dataframe-inside-a-for-loop
Python: Creating a DataFrame and Renaming It Within a For Loop
May 1, 2023 - Changing variable names with Python for loops, You probably want a dict instead of separate variables. For example. d = {} for i in range(3): d["group" + str(i)] = self.getGroup(selected, header+i) If you insist on actually modifying local variables, you could use the locals function: How to rename variables in a loop in Python ... Utilizing a pivot_table function on df can generate a new dataframe containing column headings such as jan_avg_prod_count, feb_avg_prod_count , among others, which is both swifter than a loop and more straightforward than using a dictionary.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 74694009 › update-dataframe-name-in-python-within-the-for-loop
Update dataframe name in python within the for loop - Stack Overflow
December 5, 2022 - You could simply create a list named df of DataFrames, and call them df[0], df[1]... ... NB: This assumes that the variables/dataframes (df1, df2, ..) are defined before the loop.
🌐
Posit Community
forum.posit.co › tidyverse
Produce a list of new dataframes in a for loop, then assign values - tidyverse - Posit Community
November 26, 2020 - Hi all, is there a simple way to produce a list of new dataframes and assigning it values? I have a few small dataframes, each with the same variables within (say Aus_df, Canada_df, US_df). I want to create a transposed version of each dataframe, and call them "t_original dataframe name, which ...
🌐
IncludeHelp
includehelp.com › python › create-multiple-dataframes-in-loop.aspx
Create multiple dataframes in loop in Python
October 3, 2022 - DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data. 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.
Top answer
1 of 2
5

Use a dictionary for organizing your dataframes, and groupby to split them. You can iterate through your groupby object with a dict comprehension.

Example:

>>> data
      Sport  random_data
0    soccer            0
1    soccer            3
2  football            1
3  football            1
4    soccer            4

frames = {i:dat for i, dat in data.groupby('Sport')}

You can then access your frames as you would any other dictionary value:

>>> frames['soccer']
    Sport  random_data
0  soccer            0
1  soccer            3
4  soccer            4

>>> frames['football']
      Sport  random_data
2  football            1
3  football            1
2 of 2
0

You can do this by modifying globals() but that's not really adviseable.

for S in Sports:
    globals()[str(S)] = data.loc[data['Sport']==S]    

Below is a self-contained example:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'sport':['football', 'football', 'tennis'],
                           'value':[1, 2, 3]})

In [3]: df
Out[3]: 
      sport  value
0  football      1
1  football      2
2    tennis      3

In [4]: for name in df.sport.unique():
    ...:     globals()[name] = df.loc[df.sport == name]
    ...:     

In [4]: football
Out[4]: 
      sport  value
0  football      1
1  football      2

While this is a direct answer to your question, I would recommend sacul's answer, dictionaries are meant for this (i.e. storing keys and values) and variable names inserted via globals() are usually not a good idea to begin with.

Imagine someone else or yourself in the future reading your code - all of a sudden you are using football like a pd.DataFrame which you have never explicitly defined before - how are you supposed to know what is going on?

🌐
Stack Overflow
stackoverflow.com › questions › 48521710 › python-loop-over-dataframes-and-rename-results
pandas - Python loop over dataframes and rename results - Stack Overflow
January 30, 2018 - Is this possible and is a loop the correct way to proceed? ... fn_list = ['file1', 'file2', 'file3'] # list of filenames df_list = [df1, df2, df3] # list of dataframes output_dir = r'C:\temp' for name, df in zip(fn_list, df_list): df.to_csv(os.path.join(output_dir, name+'.csv'), index=False)