To answer your question literally, globals()['dfA'] = dfNew
would define dfA
in the global namespace:
for i, row in enumerate(dfList):
dfName = dfNames[i]
dfNew = df[df['variable'] == row]
globals()[dfName] = dfNew
However, there is never a good reason to define dynamically-named variables.
If the names are not known until runtime -- that is, if the names are truly
dynamic -- then you you can't use the names in your code since your code has
to be written before runtime. So what's the point of creating a variable named
dfA
if you can't refer to it in your code?
If, on the other hand, you know before hand that you will have a variable
named dfA
, then your code isn't really dynamic. You have static variable names.
The only reason to use the loop is to cut down on boiler-plate code. However,
even in this case, there is a better alternative.
The solution is to use a dict (see below) or list1.
Adding dynamically-named variables pollutes the global namespace.
It does not generalize well. If you had 100 dynamically named variables, how
would you access them? How would you loop over them?
To "manage" dynamically named variables you would need to keep a list of their
names as strings: e.g. ['dfA', 'dfB', 'dfC',...]
and then accessed the newly
minted global variables via the globals()
dict: e.g. globals()['dfA']
. That
is awkward.
So the conclusion programmers reach through bitter experience is that
dynamically-named variables are somewhere between awkward and useless and it is
much more pleasant, powerful, practical to store key/value pairs in a dict. The
name of the variable becomes a key in the dict, and the value of the variable
becomes the value associated with the key. So, instead of having a bare name dfA
you would have a dict dfs
and you would access the dfA
DataFrame via
dfs['dfA']
:
dfs = dict()
for i, row in enumerate(dfList):
dfName = dfNames[i]
dfNew = df[df['variable'] == row]
dfs[dfName] = dfNew
or, as Jianxun Li shows,
dfs = {k: g for k, g in df.groupby('variable')}
This is why Jon Clements and Jianxun Li answered your question by showing
alternatives to defining dynamically-named variables. It's because we all
believe it is a terrible idea.
Using Jianxun Li's solution, to loop over a dict's key/value pairs you could then use:
dfs = {k: g for k, g in df.groupby('variable')}
for key, df in dfs.items():
...
or using Jon Clements' solution, to iterate through groups you could use:
grouped = df.groupby('variable')
for key, df in grouped:
...
1If the names are numbered or ordered you could use a list instead of a dict.