0

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.
4
  • if you call id(item1) and id(item28) do they give you the same number for both dataframes? Commented Apr 6, 2022 at 22:20
  • Your "duplicates" are only references to the originals, not standalone copies. If you modify a "duplicate", you're going to see that modification in the original. What are you trying to do here? Commented Apr 6, 2022 at 22:26
  • 1
    how are the variables generated? feel like a simple refactor upstream could help you more Commented Apr 6, 2022 at 22:26
  • 1
    This statement doesn't make sense: "These dataframes are created elsewhere in the program and have to be duplicated after the fact, otherwise, I would create a copy" -- you can create a copy at any time. Commented Apr 6, 2022 at 22:31

1 Answer 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
Sign up to request clarification or add additional context in comments.

2 Comments

Apologies, I only skimmed the answer and realized after-the-fact that you were applying the copy method of a df as opposed to a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.