遍历数据框名称列表

时间:2020-10-08 08:21:44

标签: python pandas dataframe loops formatting

我有一个数据框列表,每个数据框都是通过唯一的网络查询创建的;


bngimp = parse_forecast_data(get_json('419524'), None)
belimp = parse_forecast_data(get_json('419525'), None)
braimp = parse_forecast_data(get_json('419635'), None)
chilimp = parse_forecast_data(get_json('419526'), None)
chinimp = parse_forecast_data(get_json('419527'), None)
domimp = parse_forecast_data(get_json('419633'), None)
fraimp = parse_forecast_data(get_json('419636'), None)
greimp = parse_forecast_data(get_json('419528'), None)
ghaimp = parse_forecast_data(get_json('419638'), None)
indimp = parse_forecast_data(get_json('419530'), None)
indoimp = parse_forecast_data(get_json('419639'), None)
itaimp = parse_forecast_data(get_json('419533'), None)
japimp = parse_forecast_data(get_json('419534'), None)
kuwimp = parse_forecast_data(get_json('419640'), None)
litimp = parse_forecast_data(get_json('419641'), None)
meximp = parse_forecast_data(get_json('419537'), None)

我需要按照以下相同的方式格式化每个数据框;

bngimp = bngimp[['From Date','Sales Volume']]
bngimp = bngimp.set_index('From Date')
bngimp.index = pd.to_datetime(bngimp.index)
bngimp = bngimp.groupby(by=[bngimp.index.year, bngimp.index.month]).sum()
bngimp.columns = ['bngimp']

有什么方法可以遍历数据框的名称,而不必将每个数据框的名称复制并粘贴到上面的代码中?

会有更多的数据帧,因此复制和粘贴非常耗时!

非常感谢您的帮助;

1 个答案:

答案 0 :(得分:1)

我建议通过DataFrame名称为地图编号创建字典,并创建名为DataFrame的{​​{1}}字典:

out

然后通过键选择要获取的DataFrame:

d = {'419524': 'bngimp', '419525': 'belimp', ...}

out = {}
for k, v in d.items():
    df = parse_forecast_data(get_json(k), None)
    df = df[['From Date','Sales Volume']]
    df = df.set_index('From Date')
    df.index = pd.to_datetime(df.index)
    df = df.groupby(by=[df.index.year, df.index.month]).sum()
    df.columns = [v]
    out[v] = df

如果想创建一个大的DataFrame也可以使用:

print (out['bngimp'])