2016-11-08 38 views
-2

有没有办法在熊猫中生成多个DataFrames? 我想和喜欢的变量命名DataFrames如何命名带有熊猫变量的数据框

for i in range 1 to 100 
dfi in dfs 


df1= 
df2= 
df3= 

: 
: 
: 

df99= 
df100= 
+0

使用字典。 –

+2

可能的重复[如何创建可变数量的变量?](http://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) –

回答

1

我认为你可以使用dict comprehension

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

样品:

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 
+1

我是去做几个包括你的。但我不喜欢这个问题..所以我要删除我的回答 – piRSquared

+0

谢谢!这使得这里成为一个好地方! –

0

如果你真的想创建一个名为变量,你可以做以下的事情

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

但正如其他人所建议的,使用字典也许更好

相关问题