是python 2.7中exec的良好替代方案

问题描述 投票:0回答:1

我有代码,需要使用列表中的名称创建熊猫数据框。我知道这可以通过使用exec()函数来实现。但是看起来它放慢了我的应用程序。还有其他更好的选择吗?

import pandas as pd
df_names = ["first","second","third"]
col_names = ['A','B','C']
for names in df_names:
    exec("%s=pd.DataFrame(columns=col_names)"%(names))
pandas python-2.7 dataframe exec
1个回答
0
投票

发现以下方法及其对我有用

import pandas as pd
df_names = ["first","second","third"]
col_names = ['A','B','C']
d={}
for names in df_names:
    d[names]=pd.DataFrame(columns=col_names)
© www.soinside.com 2019 - 2024. All rights reserved.