I have a dataframe df with the following structure
Acatis Avantage TrueValue ValorRelativo
Date
2014-07-31 199.08 10.0 10.22 11.631
2014-08-04 198.79 10.0 10.19 11.616
2014-08-05 198.59 10.0 10.17 11.614
I need to make, with each column, a dataframe whose name is the name of the column and whose name of the only column is "Close". Each of these dataframes must have this structure.
Close
Date
2014-07-31 199.08
2014-08-04 198.79
2014-08-05 198.59
I can do the following:
acatis = df[["Acatis"]].rename (columns = {"Acatis":"Close"})
but it solves the problem only partially, because the name of the new dataframe, acatis in this example, I have to write, that is, it is not associated by default in some way. On each execution of the program I can select another list which will result in other different names and I would have to modify the names of these new df's. I've tried pivot(), stack(), and unstack() and still can't find a solution. What can be the easiest way to do it? I will appreciate your help
Really what you are looking for is to create variables dynamically, this is possible in Python, although it is not generally recommended:
Using
exec
:exec
it simply receives a string that is valid Python code and executes it. We just have to format the string appropriately.Using the dictionaries directly
globals
orlocals
depending on the namespace where we want to create the variables:With this you have created a variable for each column, with the same name as the column, which refers to the new corresponding DataFrame:
The correct and most commonly accepted way to do what you want is to use a dictionary, where the keys are the identifier of each DataFrame and the value is the DataFrame:
If you want to access each DataFrame as an attribute, you should create your own namespace. You can use a class that acts as a container, with each DataFrame being attributes of it, or use
types.SimpleNamespaces
:You can do the same using a class as a container:
Another solution, since you use Pandas, is to use a Pandas Series to store the DataFrames:
Note
If you want the identifier/key to be lowercase (
dfs["acatis"
] /dfs.acatis
) usestr.lower
: