0

Is there a way to generate multiple DataFrames in Pandas? I want to name the DataFrames with variables like:

for i in range 1 to 100
dfi in dfs


df1=
df2=
df3=

:
:
:

df99=
df100=
Marcs
  • 3,768
  • 5
  • 33
  • 42
Taro Nikkei
  • 29
  • 1
  • 1
  • 2

2 Answers2

4

I think you can use dict comprehension:

N = 101 # 5 in sample
dfs = {'name' + str(i):df for i in range(1,N)}
print (dfs)

Sample:

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3

N = 5
dfs = {'name' + str(i):df for i in range(1,N)}
print (dfs)
{'name3':    A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3, 'name4':    A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3, 'name2':    A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3, 'name1':    A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3}

print (dfs['name1'])
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

If you really want to create named variables, you can do the following

variables = locals()
for i in range(100):
    variables["df{0}".format(i)] = ...

But as others suggested, using a dictionary is perhaps better

honza_p
  • 2,073
  • 1
  • 23
  • 37