0

I have several dataframes, such as df1, df2 etc. then i made a list of the names of these df's like lst = ['df1', 'df2']

Now, I want to use these df's in a function "my_func()"

my_func(dataframe=df1) 

my_func(dataframe=df2)

...

How can i express the list of dataframes in a loop, so that i can call different df's in my_func? (the way below does't work, since I'm calling strings instead of df's...)

for i in lst:
    my_func(dataframe=i)

Thanks!

George Liu
  • 3,601
  • 10
  • 43
  • 69
  • 1
    You should look into [dictionaries](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) instead. Where you'd do `data_frames = {'df1' : [], 'df2' : []}` and you could access and pass them straight into functions. You could even do `for df in data_frames.keys(): my_func(dataframe=my_dataframes[df])` – Torxed Nov 15 '16 at 19:56
  • Indeed, no need to flood your global environment with many dfs. Contain all in one indexed object like dictionary. – Parfait Nov 15 '16 at 20:02

2 Answers2

1

I would argue that storing the name of your data-sets as strings, and then try to pass the string as a argument is counter productive.

Instead, what I would do is create a dictionary with all your data-sets as values to string keys, and pass the dictionary objects into your function.

my_frames = {'df1' : [1,2,3], 'df2' : [4,5,6]}

def my_func(dataframe):
    ... do work on dataframe ...

for df_name, df_obj in my_frames.items():
    print('Doing work on:', df_name)
    my_func(df_obj)

Another nifty thing with dictionaries and functions is that you can expand the dictionary as named parameters right off the bat.

my_frames = {'temperature' : 25.5, 'wind' : 4.6}

def weather(temperature=None, wind=None):
    print('The temp is:', temperature, 'and the wind speed is:', wind)

weather(**my_frames)

But that's just a little side note.

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • But in this case, i have all my dataframes ready made, and i don't want to do extra work to convert them... – George Liu Nov 15 '16 at 20:02
  • @GeorgeLiu You don't really need to convert them, dictionaries is more a place-holder/storage utility. You can add the lists as is by doing `my_frames = {'df1' : df1, 'df2' : df2}`. And also it would be wise to refactor your code so they end up in the dictionary right way.. Again no need to convert as much as store them differently. The end result can be the same and the build step can be the same, it's the "where do i put them" that's different perhaps. – Torxed Nov 15 '16 at 20:04
  • i see. so basically, you're saying a df can be a value in a dict, right? – George Liu Nov 15 '16 at 20:10
  • @GeorgeLiu Absolutely, that's the beauty of dictionaries. – Torxed Nov 15 '16 at 20:11
  • @GeorgeLiu No worries mate, keep on asking things if you need help, that's what we are here for. – Torxed Nov 15 '16 at 20:12
0

So i figured out...Just need to use eval():

for i in lst:
    my_func(dataframe=eval(i))
George Liu
  • 3,601
  • 10
  • 43
  • 69
  • 2
    Beware the shitstorm of comments because you used `eval()` without even describing the hazards of it. Don't do it, unless you can specify why you need to. [Is using eval in Python a bad practice?](http://stackoverflow.com/questions/1832940/is-using-eval-in-python-a-bad-practice) – Torxed Nov 15 '16 at 20:00