2

This seems to be a very common question, but my question is slightly different. Most of the questions in SO I have searched for; gives how to create different variables iteratively. But I want to use those variables iteratively which are already in a dictionary lets say.

Consider using pandas, let us say I define 3 dataframes df_E1, df_E2, df_E3. Each of them has columns like name, date, purpose.

Let's say I want to print describe from all of them. df_E1.describe(), df_E2.describe(), df_E3.describe() . Now, instead of printing one by one, what if I have 20-30 of such dataframes and I want to describe from df_E{number}.describe() in a for loop. How can I do that?

df_E1 = {'name':['john','tom','barney','freddie'], 'number':['3','4','5','6'], 'description':['a','b','c','d']}
df_E2 = {'name':['andy','candy','bruno','mars'], 'number':['1','2','5','8'], 'description':['g','h','j','k']}
df_E3 = {'name':['donald','trump','harry','thomas'], 'number':['9','4','5','7'], 'description':['c','g','j','r']}
df_E1 = pd.DataFrame(df_E1)
df_E2 = pd.DataFrame(df_E2)
df_E3 = pd.DataFrame(df_E3)
print(df_E1.head())
print(df_E2.head())
print(df_E3.head())

#### instead of above three statements, is there any way I can print them in a 
#### for loop. Below does not work as it gives just the string printed.
for i in range(1,4):
  print(str('df_E')+str(i))

### Now if I am able to print dataframes somehow, I will be able to use all 
### these in a loop. Eg. if I need to print describe of these, I will be able 
### to do it in for loop:

for i in range(1,4):
  print((str('df_E')+str(i)).describe()) // something like this which works

This isn't reflecting the already asked questions as they focus more on just creating the variables as string in for loop, which I know can be done using a dictionary. But here, the requirement is to use already present variables

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Tushar Seth
  • 563
  • 7
  • 15

2 Answers2

4

Here the best is use dict:

d = {'df_E1':df_E1, 'df_E2':df_E2, 'df_E3':df_E3}
print (d)
{'df_E1':       name number description
0     john      3           a
1      tom      4           b
2   barney      5           c
3  freddie      6           d, 'df_E2':     name number description
0   andy      1           g
1  candy      2           h
2  bruno      5           j
3   mars      8           k, 'df_E3':      name number description
0  donald      9           c
1   trump      4           g
2   harry      5           j
3  thomas      7           r}

for k, v in d.items():
    print (v.describe())
        name number description
count      4      4           4
unique     4      4           4
top     john      5           b
freq       1      1           1
         name number description
count       4      4           4
unique      4      4           4
top     bruno      5           g
freq        1      1           1
         name number description
count       4      4           4
unique      4      4           4
top     harry      5           r
freq        1      1           1

But is it possible, but not recommended, with globals:

for i in range(1,4):
    print(globals()[str('df_E')+str(i)])

      name number description
0     john      3           a
1      tom      4           b
2   barney      5           c
3  freddie      6           d
    name number description
0   andy      1           g
1  candy      2           h
2  bruno      5           j
3   mars      8           k
     name number description
0  donald      9           c
1   trump      4           g
2   harry      5           j
3  thomas      7           r
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • thanks it was the first way which you mentioned which i was looking for but one thing can be added as I mentioned in code below so that we get dictionary in order (as in dictionary, order might not be maintained {yes, orderedDict could be used}): ``` for i in range(len(d)): print (d[str('df_E')+str(i)].describe()) ``` – Tushar Seth Aug 21 '19 at 05:04
  • @TusharSeth - yes, but necessary create dictionary first. Also order depends of version of python, if use python [3.6](https://stackoverflow.com/a/39424969) then built-in dict will be ordered, if not `orderedDict `. – jezrael Aug 21 '19 at 05:11
0

You can do this simply by using the eval() function, but it is frowned upon :

for i in range(1, 4):
    print(eval('df_E{}.describe()'.format(i)))

Here is a link on why it's considered a bad practice

vlemaistre
  • 3,301
  • 13
  • 30