0

I was performing some operations (successfully) outside of a function:

x = df.copy
do stuff to get df1 and df2 
for f in ['df1','df2']:
   do stuff
   vars()[f] = stuff

This works fine. And the only reason I'm using vars() here is because it's the only way I can figure out how to save each dataframe simply after looping since 'f = stuff' won't save the result to the original dataframe. But whenever I throw this in a function, it seems to break. The variable doesn't save ie df1 remains the same state it was before the for-loop. Any advice?

def func (df):
   x = df.copy
   do stuff to get df1 and df2 
   for f in ['df1','df2']:
      do stuff
      vars()[f] = stuff
   df = pd.concat((df1,df2),axis=0)
   return df
chicagobeast12
  • 643
  • 1
  • 5
  • 20
  • Inside the function you should use either `locals()` or `globals()`. – Barmar Apr 11 '22 at 23:22
  • 1
    But you shouldn't use dynamic variables. Use a dictionary to hold values keyed by a string. – Barmar Apr 11 '22 at 23:23
  • I've tried locals and globals, but still didn't work. I was just hoping to use this method to avoid having to create a dictionary, but I can definitely do that if it comes down to it. Was just hoping to make a small tweak to the existing code – chicagobeast12 Apr 11 '22 at 23:25
  • Related: [How do I create variable variables?](https://stackoverflow.com/q/1373164/4518341) – wjandrea Apr 11 '22 at 23:25
  • Why do you `do stuff to get df1 and df2` if `df1` and `df2` are never accessed before being overwritten? Or is that part of `stuff`? Please make a [mre]. You might actually want something like `df1, df2 = map(stuff, [df1, df2])` – wjandrea Apr 11 '22 at 23:30
  • it is part of the stuff, my question isn't focused on that, it's focused on why vars() won't work inside the function, or if there's a simple way to loop over multiple dataframes and save the output. This is really the driver to why I used this method in the first place – chicagobeast12 Apr 11 '22 at 23:36

1 Answers1

1

There's no need to assign the dataframes to variables. Just put them in a list, and pass that to pd.concat().

def func (df):
   x = df.copy
   do stuff to get df1 and df2 
   df_list = []
   for f in ['df1','df2']:
      do stuff
      df_list.append(stuff)
   df = pd.concat(df_list,axis=0)
   return df
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `for f in ['df1','df2']` should probably change too, like `for f in df1, df2`, or a step further, make a list in the first place, something like `do stuff to get df_list_0` followed by `for f in df_list_0` – wjandrea Apr 11 '22 at 23:27
  • Oh wait, the question's unclear on that. `df1` and `df2` are never accessed before being overwritten – wjandrea Apr 11 '22 at 23:29