简体   繁体   English

在数据框名称中添加前缀/后缀或在python中将“参数”传递至数据框名称

[英]Add prefix / suffix to dataframe name or pass 'parameter' into dataframe name in python

How can I pass parameter in python to create new dataframe each time for example : 我如何每次在python中传递参数来创建新的数据框,例如:

import numpy as np

for i in range(10):
    df = DataFrame(np.random.rand(10, 3), columns=list('abc'))
    print i , df

This will give me a different dataframe df each time, but I want my df output to be dataframe named : 每次都会给我一个不同的数据帧df,但是我希望我的df输出是名为的数据帧:

df1 , df2, df3 , df4 ..

=============================================== ==============================================

import numpy as np
name = ['one','two','three']
for i in range(3):
    name_df = DataFrame(np.random.rand(10, 3), columns=list('abc'))
    print i , name_df

This second example I would like my dataframe names to be 第二个示例我希望我的数据框名称为

one, two, three

================================================ ===============================================

here below as a function 下面是功能

def data(t):
    df_%s , %t = DataFrame(np.random.rand(10, 3), columns=list('abc'))
    print df_%s , %t
data('a')
data('b')

here in the function I would like to get output dataframe named : 在此函数中,我想获取名为的输出数据框:

df_a , df_b

I don't know how you can name exactly as you have mentioned but you can instead create dictionary of dataframe like this: 我不知道如何准确地命名,但是可以像这样创建数据字典:

>>> df={}
>>> for i in range(5):
    df[str(i)] = pd.DataFrame(np.random.rand(10, 3), columns=list('abc'))

And access them as df['0'],df['1'], etc. 并以df['0'],df['1'], etc.访问它们df['0'],df['1'], etc.

If you must need to name it like that you can use this, 如果您需要像这样命名,可以使用此名称,

for i in range(5):
    vars()['df_' +str(i)] = pd.DataFrame(np.random.rand(10, 3), columns=list('abc'))

And if you need to set the name withing a function it will only be available withing the function as it will a local variable. 而且,如果您需要通过函数设置名称,那么它将仅随函数一起使用,因为它将是局部变量。 In which case you can use the dict one. 在这种情况下,您可以使用dict。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM