0

I have a function that returns a dict of pandas dataframes. The dict has a key and the key for each dataframe is its corresponding name.

def fun(inputs): x = calculations y = calculations z = calculations, etc. return: {'name1':x,'name2':y,'name3':z} 

I would like to use some of these dataframes as variables elsewhere in my code. To do this, I currently do the following:

x = fun() var1 = x['name1'] var2 = x['name2'] var3 = x['name3'] 

and so on. I would like to do this more efficiently (fewer lines of code). In my mind, I should be able to do something like this:

[var1,var2,var3] = fun() 

But this just assigns each name to each variable as a string. Can this be done? or should my function output be setup differently?

5
  • 2
    Why not just use the dictionary? Commented Apr 25, 2017 at 20:19
  • 1
    @juanpa.arrivillaga: Reaching for the [''] keys all the time is awkward, it makes your statements bulkier, and your IDE won't be as helpful with dict entries as with variables. Commented Apr 25, 2017 at 20:22
  • 1
    Then don't use a dictionary, and return a tuple and use tuple unpacking assignment. Commented Apr 25, 2017 at 20:24
  • 1
    @juanpa.arrivillaga: But then you have to remember which slots you put everything in, and you're stuck with whatever order you pick. namedtuples can help, but they still have issues. Commented Apr 25, 2017 at 20:29
  • ... OK, I've never found it too difficult to keep it all organized, especially if you use namedtuples. But regardless, look at the functionality the OP is imagining utilizing this: [var1,var2,var3] = fun() how does this in any way take advantage of the nice properties of dictionaries we all know and love? Commented Apr 25, 2017 at 20:33

1 Answer 1

4

namedtuple

  • import the namedtuple function from collections
  • Use it to create a new class that you can then use to instantiate new class members
  • Accessing the named elements now utilizes the dot notation.

from collections import namedtuple mydfs = namedtuple('MyDFs', ['name1', 'name2', 'name3']) def fun(inputs): x = calculations y = calculations z = calculations, etc. return mydfs(x, y, z) x = fun(inputs) x.name1 

If you want, you can also unpack one of these:

var1, var2, var3 = x 

Old Answer

Easiest way is to pass that dictionary to the pd.Panel constructor

pn = pd.Panel({'name1':x,'name2':y,'name3':z}) 

This is can now be thought of as a series of dataframes. Each dataframe can be accessed via pn['name1'] etc.

Sign up to request clarification or add additional context in comments.

5 Comments

So... we access the dataframes exactly the same way we would have with the dict? It's not clear how this gets us anywhere.
@user2357112 True. It's more convenient cohesive structure. But access is identical. I'll have to rethink it.
pd.Panel is also facing an oncoming depreciation, I recently learned.
@Mitch that stinks... But I guess I'll get over it.
@piRSquared Need to warm up to xarray integration! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.