Just to underline my comment to @maxymoo's answer, it's almost invariably a bad idea ("code smell") to add names dynamically to a Python namespace. There are a number of reasons, the most salient being:

  1. Created names might easily conflict with variables already used by your logic.

  2. Since the names are dynamically created, you typically also end up using dynamic techniques to retrieve the data.

This is why dicts were included in the language. The correct way to proceed is:

d = {}
for name in companies:
    d[name] = pd.DataFrame()

Nowadays you can write a single dict comprehension expression to do the same thing, but some people find it less readable:

d = {name: pd.DataFrame() for name in companies}

Once d is created the DataFrame for company x can be retrieved as d[x], so you can look up a specific company quite easily. To operate on all companies you would typically use a loop like:

for name, df in d.items():
    # operate on DataFrame 'df' for company 'name'

In Python 2 you were better writing

for name, df in d.iteritems():

because this avoids instantiating the list of (name, df) tuples that .items() creates in the older version. That's now largely of historical interest, though there will of course be Python 2 applications still extant and requiring (hopefully occasional) maintenance.

Answer from holdenweb on Stack Overflow
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ multiple dataframes in a loop using python
Multiple Dataframes in a Loop Using Python - AskPython
March 31, 2023 - So, after printing the dictionary, we can see that empty dataframes are created for each element of the list. Here, we have not entered any data for each column so itโ€™ll be printed as empty data columns. ... This way, we can create multiple data frames using a loop in Python language.
Discussions

Create a for loop to make multiple data frames?
I'm having trouble making a loop that will iterate through my data and create multiple data frames. Here's some dummy data: mydf More on forum.posit.co
๐ŸŒ forum.posit.co
0
1
June 3, 2021
Python Looping multiple dataframes
I recommend using matplotlib when plotting, specifically its objected-oriented approach. From here you should be able to save the axes object for later. More on reddit.com
๐ŸŒ r/learnpython
5
6
November 3, 2021
python 3.x - Creating multiple dataframes with a loop - Stack Overflow
This undoubtedly reflects lack of knowledge on my part, but I can't find anything online to help. I am very new to programming. I want to load 6 csvs and do a few things to them before combining them More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 20, 2018
How can I create multiple dataframes at the same time using a single script?
You can use something like: for i, data in enumerate(data_list): df_name = f'df{i + 1}' data_frames[df_name] = pd.DataFrame(data) to dynamically create data frames, or you can use dictionary comprehension: data_frames = {name: pd.DataFrame(data) for name, data in data_frames_data.items()} More on reddit.com
๐ŸŒ r/learnpython
5
5
September 25, 2023
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ create-multiple-dataframes-in-loop.aspx
Create multiple dataframes in loop in Python
October 3, 2022 - 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. ... # Import pandas as pd import pandas as pd # Creating list l = ['Mango','Apple','Grapes','Orange','Papaya'] # Creating ...
๐ŸŒ
Posit Community
forum.posit.co โ€บ general
Create a for loop to make multiple data frames? - General - Posit Community
June 3, 2021 - I'm having trouble making a loop that will iterate through my data and create multiple data frames. Here's some dummy data: mydf <- data.frame("color"=c("blue","yellow","red","green","pink","orange","cyan"), โ€ฆ
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python looping multiple dataframes
r/learnpython on Reddit: Python Looping multiple dataframes
November 3, 2021 -

I am learning python and is having trouble accessing data from multiple dataframes.

I want to make multiple bar plot with different dataframes. All of the dataframes have the same columns. So I thought, instead of writing the code one by one, maybe I could somehow iterate through the dataframes. But I haven't find the right way to do it. Could anyone advice me? I am curious if it can be done in one go instead of writing it for every dataframe.

Top answer
1 of 3
3

I think you think your code is doing something that it is not actually doing.

Specifically, this line: df = pd.read_csv(file)

You might think that in each iteration through the for loop this line is being executed and modified with df being replaced with a string in dfs and file being replaced with a filename in files. While the latter is true, the former is not.

Each iteration through the for loop is reading a csv file and storing it in the variable df effectively overwriting the csv file that was read in during the previous for loop. In other words, df in your for loop is not being replaced with the variable names you defined in dfs.

The key takeaway here is that strings (e.g., 'df1', 'df2', etc.) cannot be substituted and used as variable names when executing code.

One way to achieve the result you want is store each csv file read by pd.read_csv() in a dictionary, where the key is name of the dataframe (e.g., 'df1', 'df2', etc.) and value is the dataframe returned by pd.read_csv().

list_of_dfs = {}
for df, file in zip(dfs, files):
    list_of_dfs[df] = pd.read_csv(file)
    print(list_of_dfs[df].shape)
    print(list_of_dfs[df].dtypes)
    print(list(list_of_dfs[df]))

You can then reference each of your dataframes like this:

print(list_of_dfs['df1'])
print(list_of_dfs['df2'])

You can learn more about dictionaries here:

https://docs.python.org/3.6/tutorial/datastructures.html#dictionaries

2 of 3
3

Use dictionary to store you DataFrames and access them by name

files = ('data1.csv', 'data2.csv', 'data3.csv', 'data4.csv', 'data5.csv', 'data6.csv')
dfs_names = ('df1', 'df2', 'df3', 'df4', 'df5', 'df6')
dfs ={}
for dfn,file in zip(dfs_names, files):
    dfs[dfn] = pd.read_csv(file)
    print(dfs[dfn].shape)
    print(dfs[dfn].dtypes)
print(dfs['df3'])

Use list to store you DataFrames and access them by index

files = ('data1.csv', 'data2.csv', 'data3.csv', 'data4.csv', 'data5.csv', 'data6.csv')
dfs = []
for file in  files:
    dfs.append( pd.read_csv(file))
    print(dfs[len(dfs)-1].shape)
    print(dfs[len(dfs)-1].dtypes)
print (dfs[2])

Do not store intermediate DataFrame, just process them and add to resulting DataFrame.

files = ('data1.csv', 'data2.csv', 'data3.csv', 'data4.csv', 'data5.csv', 'data6.csv')
df = pd.DataFrame()
for file in  files:
    df_n =  pd.read_csv(file)
    print(df_n.shape)
    print(df_n.dtypes)
    # do you want to do
    df = df.append(df_n)
print (df)

If you will process them differently, then you do not need a general structure to store them. Do it simply independent.

df = pd.DataFrame()
def do_general_stuff(d): #here we do common things with DataFrame
    print(d.shape,d.dtypes)

df1 = pd.read_csv("data1.csv")
# do you want to with df1

do_general_stuff(df1)
df = df.append(df1)
del df1

df2 = pd.read_csv("data2.csv")
# do you want to with df2

do_general_stuff(df2)
df = df.append(df2)
del df2

df3 = pd.read_csv("data3.csv")
# do you want to with df3

do_general_stuff(df3)
df = df.append(df3)
del df3

# ... and so on

And one geeky way, but don't ask how it works:)

from collections import namedtuple
files = ['data1.csv', 'data2.csv', 'data3.csv', 'data4.csv', 'data5.csv', 'data6.csv']

df = namedtuple('Cdfs',
                ['df1', 'df2', 'df3', 'df4', 'df5', 'df6']
               )(*[pd.read_csv(file) for file in files])

for df_n in df._fields:
    print(getattr(df, df_n).shape,getattr(df, df_n).dtypes)

print(df.df3)
๐ŸŒ
Kaggle
kaggle.com โ€บ questions-and-answers โ€บ 93779
Looping multiple dataframes?
Checking your browser before accessing www.kaggle.com ยท Click here if you are not automatically redirected after 5 seconds
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how can i create multiple dataframes at the same time using a single script?
r/learnpython on Reddit: How can I create multiple dataframes at the same time using a single script?
September 25, 2023 -

I'm currently working on simulated data. For each simulation, I have to create a dataframe with around 1500-4500 rows each. All those rows depend on the data of previous rows, so I must iterate over the dataframe to create a new row. I want to repeat this process 500 times, but each of these instances ale completely independent from eachother.

I have scrpits to generate all this data, but it takes too much time since my scripts are only able to run one simulation at a time. Is it possible for a single script to run each simulation in parallel so I can merge then into a single .csv file at the end of all calculations?

๐ŸŒ
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 - I have a list of dataframes (for this example 2) and want to apply a for-loop to the list of frames to generate 2 new dataframes. To start, here is my starting dataframe called df_final: First, I create 2 dataframes: df2_b2c_fast, df2_b2b_fast: for x in df_final['b2b_b2c_prod'].unique(): local...
๐ŸŒ
Cnasolution
cnasolution.com โ€บ questions โ€บ 196553 โ€บ create-multiple-dataframes-in-loop
cna solution | Create multiple dataframes in loop
February 20, 2020 - To operate on all companies you would typically use a loop like: for name, df in d.items(): # operate on DataFrame 'df' for company 'name' In Python 2 you are better writing for name, df in d.iteritems(): because this avoids instantiating a list of (name, df) tuples. Adding to the above great answers. The above will work flawless if you need to create empty data frames but if you need to create multiple dataframe based on some filtering: Suppose the list you got is a column of some dataframe and you want to make multiple data frames for each unique companies fro the bigger data frame:- First t
Top answer
1 of 1
3

First: I think you want the product functionality, not zip, since you are checking every df with every ref. In zip, you would check df_a with ref_1 and df_b with ref_2 only.

Second: Your can look at the equation $(1+2+3+4)โˆ’(5+5+5+5)$ as $(1-5) + (2-5) + ...$ which is simply subtracting data frames and sum over columns.

With these two consideration, assuming you have defined your objects as follows:

df_a = {
    'name': 'df_a',
    'value': pd.DataFrame([[1, 2, 3, 4], [2, 4, 6, 8]])
}
df_b = {
    'name': 'df_b',
    'value': pd.DataFrame([[10, 5, 2, 1], [4, 4, 6, 2]])
}

ref_1 = {
    'name': 'ref_1',
    'value': pd.DataFrame([[5, 5, 5, 5], [5, 5, 5, 5]])
}
ref_2 = {
    'name': 'ref_b',
    'value': pd.DataFrame([[3, 3, 3, 3], [3, 3, 3, 3]])
}

I did this because I want to use the names in creating the name of the columns of your final df. Then your code would be:

from itertools import product

final_result = pd.DataFrame(
    {
        '{}_{}'.format(df['name'], ref['name']): (df['value']-ref['value']).sum(axis=1)
        for (df, ref) in product([df_a, df_b], [ref_1, ref_2])
    }
)
  • I have used dictionary comprehension to skip the ugly loop/append solution.
  • product function from itertools does your iteration. product on (ab, cd) gives you ac, ad, bc, bd
  • as for keys, df names are joined together with _, and as for values, I have subtracted two dfs and sum over columns (axis=1)

The result would then be as you expect:

   df_a_ref_1  df_a_ref_b  df_b_ref_1  df_b_ref_b
0         -10          -2          -2           6
1           0           8          -4           4

Still if you want to expand the dictionary comprehension or do not want to define dictionaries of names/values, of course you can imagine how you can write simple for loops with the same logic:

for (df, ref) in product([df_a, df_b], [ref_1, ref_2]):
    # your desired columns
    col = (df - ref).sum(axis=1)
Top answer
1 of 3
2

This should do it:

for i in province_id:
    for j in year:
        locals()['sub_data_{}_{}'.format(i,j)] = data[(data.provid==i) & (data.wave==j)]

I initially suggested using exec, which is not usually considered best practice for safety reasons. Having said so, if your code is not exposed to anyone with malicious intentions, it should be OK, and I'll leave it here for the sake of completeness:

for i in province_id:
    for j in year:
        exec "sub_data_{}_{} = data[(data.provid==i) & (data.wave==j)]".format(i,j)

Nevertheless, for most use cases, it's probably better to use a collection of some sort, e.g. a dictionary, because it will be cumbersome to reference dynamically generated variable names in subsequent parts of your code. It's also a one-liner:

data_dict = {key:g for key,g in data.groupby(['provid','wave'])}
2 of 3
2

I think the best is create dictionary of DataFrames with groupby with filtering first by boolean indexing:

df = pd.DataFrame({'A':list('abcdef'),
                   'wave':[2004,2005,2004,2005,2005,2004],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'provid':list('aaabbb')})

print (df)
   A  C  D  E provid  wave
0  a  7  1  5      a  2004
1  b  8  3  3      a  2005
2  c  9  5  6      a  2004
3  d  4  7  9      b  2005
4  e  2  1  2      b  2005
5  f  3  0  4      b  2004


province_id = ['a','b']
year = [2004]
df = df[(df.provid.isin(province_id)) &(df.wave.isin(year))]
print (df)
   A  C  D  E provid  wave
0  a  7  1  5      a  2004
2  c  9  5  6      a  2004
5  f  3  0  4      b  2004

dfs = {'{0[0]}_{0[1]}'.format(i) : x for i, x in df.groupby(['provid','wave'])}

Another solution:

dfs = dict(tuple(df.groupby(df['provid'] + '_' + df['wave'].astype(str))))

print (dfs)
{'a_2004':    A  C  D  E provid  wave
0  a  7  1  5      a  2004
2  c  9  5  6      a  2004, 'b_2004':    A  C  D  E provid  wave
5  f  3  0  4      b  2004}

Last you can select each DataFrame:

print (dfs['b_2004'])
   A  C  D  E provid  wave
5  f  3  0  4      b  2004

Your answer should be changed by:

sub_data = {}
province_id = ['a','b']
year = [2004]
for i in province_id:
    for j in year:
         sub_data[i + '_' + str(j)] = df[(df.provid==i) &(df.wave==j)]

print (sub_data)
{'a_2004':    A  C  D  E provid  wave
0  a  7  1  5      a  2004
2  c  9  5  6      a  2004, 'b_2004':    A  C  D  E provid  wave
5  f  3  0  4      b  2004}
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 70069743 โ€บ create-multiple-dataframes-with-a-for-loop
python - Create multiple DataFrames with a for loop - Stack Overflow
data = pd.DataFrame([['2021-11-16','250'],['2021-11-17','225'],['2021-11-18','200'],['2021-11-19','175'],['2021-11-20','150'],['2021-11-21','125']], columns = ['export_date','price']) curr_price = 100 cutoffs = [2,4,6] for c in cutoffs: cutoff_date ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ efficient pandas code when creating multiple dataframes from two initial dataframes
r/learnpython on Reddit: Efficient pandas code when creating multiple dataframes from two initial dataframes
June 25, 2018 -

I have two dataframes from which i want to create multiple new dataframes. My code currently looks like this:

import pandas as pd
df_h = pd.read_csv('filename1.csv',skiprows=6)
df_c = pd.read_csv('filename2.csv', skiprows=6)

merged_tables, sheet_titles = ( [] for i in range(2))

c1 = df_c[(df_c['Document'].str.startswith("AB")) & (df_c['Symbol '] == "ARD")]
h1 = df_h[df_h["Code "] == 7]
h1.at['Total', 'Amount '] = h1['Amount '].sum()
c1.at['Total', 'Amount '] = c1['Amount '].sum()
h1.reset_index(drop=True, inplace=True)
c1.reset_index(drop=True, inplace=True)
merged_table1 = pd.concat([h1,c1],axis=1)
merged_tables.append(merged_table1)
sheet_titles.append(7)

So what I'm doing is basically checking two conditions in first dataframe, one condition in second dataframe and assigning it as new dataframes. Then i'm adding new row to sum one column, reseting index in both dataframes, merging them and appending new dataframe to a list, which i'm later using to create excel file from it.

But i want to create more new dataframes like this:

c10 = df_c[(df_c['Document'].str.startswith("CD")) & (df_c['Symbol '] == "ARD")]
h10 = df_h[df_h["Code "] == 23]
h10.at['Total', 'Amount '] = h10['Amount '].sum()
c10.at['Total', 'Amount '] = c10['Amount '].sum()
h10.reset_index(drop=True, inplace=True)
c10.reset_index(drop=True, inplace=True)
merged_table10 = pd.concat([h10,c10],axis=1)
merged_tables.append(merged_table10)
sheet_titles.append(23)

c19 = df_c[(df_c['Document'].str.startswith("EF")) & (df_c['Symbol '] == "ARD")]
h19 = df_h[df_h["Code "] == 30]
h19.at['Total', 'Amount '] = h19['Amount '].sum()
c19.at['Total', 'Amount '] = c19['Amount '].sum()
h19.reset_index(drop=True, inplace=True)
c19.reset_index(drop=True, inplace=True)
merged_table19 = pd.concat([h19,c19],axis=1)
merged_tables.append(merged_table19)
sheet_titles.append(30)

Currently i'm just explicitly repeating the same code for all new dataframes that i want to create, only changing the conditions and variables name, as i don't know how to wrap my head around writing some for loop to it and reducing amount of code.

Basically, what's always changing for each new dataframe are starting characters from first condition, code number from second and sheet title that's being appended to a list. All the other operations, so suming a column, reseting index, merging selected tables and appending it to a list will always remain the same.

If it would be a csv file or data stored in a different lists, i would just make for loop with many elifs, but as it's pandas dataframe and instead of every element you're usually accessing whole column, i don't know how to write it efficently, as i know that writing up that many variables and repeating such amount of code isn't very efficent.

I know that i have to declare those changing conditions anyway, but wrapping it up in some concise for loop or function would definitely make it more efficient and scalable.

๐ŸŒ
Medium
medium.com โ€บ @zeebrockeraa โ€บ create-list-of-dataframes-for-loop-python-b64acb9369e2
Create List of dataframes For Loop Python | by Zeeshan Ali | Medium
July 15, 2023 - # Create an empty list to store ... 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 ...