'Create duplicate dataframes from list of existing dataframes python

Updated for Clarity

Task

  • Create a set of duplicate dataframes with a different set of names, such as item28, item30, item31...item38.
  • After creating the new copies, I then have to modify the contents of specific columns.

Current Process

  • Step 1: Create new dataframes by copying the original:
item28 = item1.copy(); 
item29 = item2.copy(); 
item30 = item3.copy(); 
item31 = item4.copy(); 
item32 = item5.copy(); 
item33 = item6.copy(); 
item34 = item7.copy(); 
item35 = item8.copy(); 
item36 = item9.copy(); 
item37 = item10.copy(); 
item38 = item11.copy();
  • Step 2. Modify the columns in the new data frame: item28['indicator_number'] = 28;

Goal

  • Reproduce this manual process in a dynamic, Pythonic way.

Thanks for your help!



Solution 1:[1]

The "Pythonic" way to generate your duplicates:

list_of_copies = [df.copy() for df in list_of_originals]

Note the use of .copy() -- this is necessary because otherwise you're just creating duplicate references to the same dataframes. Any change made in the "duplicate" would be reflected in the original which is almost certainly not what you want (otherwise what would be the point?).

If you similarly have a list of values you want to use to update the dataframe copies:

for new_value, df in zip(list_of_values, list_of_copies):
    df["indicator_number"] = new_value

Right now it's not super clear what you're trying to do here, so I'm kinda just grasping at straws. Please provide a minimal reproducible example if you want a more detailed answer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ddejohn