Should I use the dictionary or the series to hold a bunch of dataframe?

Suppose I have several dataframes: df1, df2, df3, etc. The label with each dataframes is A1, A2, A3 etc. I want to use this information as a whole, so that I can pass them. Three methods came into my mind:

method 1

use a label list: labels=["A1", "A2", "A3"...] and a list of dataframes dfs=[df1, df2, df3...].

method 2

use a dictionary: d={"A1": df1, "A2": df2, "A3": df3}.

method 3

use a pandas series: s=pd.Series([df1, df2, df3], index=["A1", "A2", "A3"]).

I will use the label and dataframes sequentially, therefore I think method1 and method3 should be my choice. However, using method 1 will need me to pass two items, while using method 3 I only need to keep one object. Is it a common practice to put the dataframes in a series? I seldom see people do this, is it against best practice? Is there any better suggestions?

Upvotes: 3

Views: 550

Answers (2)

Stuart
Stuart

Reputation: 9858

An OrderedDict would probably be more conventional than using a series for this.

from collections import OrderedDict
d = OrderedDict([("A1", df1), ("A2", df2), ("A3", df3)])

This can easily be iterated over:

for label, df in d:
    print(label, df)

That said I can't see any strong reason not to use a pandas series. A small advantage of using the series is that you can access the dataframes using dot notation s.A1, s.A2 etc. as well as using the dictionary-like notation s["A1"]. Using a series, it would also be relatively easy to sort the dataframes, insert additional ones in the middle, or associate additional metadata with them later if needed.

(See this question on dictionary ordering in Python 3.6 and 3.7 - you may be able to use an ordinary dictionary instead of an OrderedDict if using Python 3.7 and you don't need to use other 'ordered' behaviours. In Python 3.6, the preservation of insertion order is an implementation detail and should not be relied upon.)

Upvotes: 5

korakot
korakot

Reputation: 40828

Method 2 also works. Since Python 3.6 it remembers the order it is created too.

Upvotes: 2

Related Questions