繁体   English   中英

将行附加到 dataframe inside dict

[英]Appending rows to dataframe inside dict

我正在构建一个for loop ,我想在其中构建一个包含 2 个数据帧的字典。 我的循环是为每次迭代创建具有 1 列和 24 行的数据帧,它在 2 个不同的年份和 4 个单独的列中这样做。

到目前为止,我已经设法将数据帧保存到 2 个键中,并为每个 dataframe 添加了 4 列,但仅限于前 24 个观察值。 现在,我需要 append 行到每个 dataframe 和for loop期间的每一列。

这意味着,现在我有 2 个形状为24*4的键。 但我想继续附加它们,以便它们获得48*472*4等形状。

我开发的代码看起来像这样(你可以忽略for术语,我只是为这个例子写的)。

for i in year:
    for j in days:
        for column in range(1,5):
            if year == 17544 and s == 1:
                dff_dict['expert_2021']['Price_REG1']=c
            elif year == 17544 and s == 2:
                dff_dict['expert_2021']['Price_REG2']=c
            elif year == 17544 and s == 3:
                dff_dict['expert_2021']['Price_REG3']=c
            elif year == 17544 and s == 4:
                dff_dict['expert_2021']['Price_REG4']=c
            elif year == 26304 and s == 1:
                dff_dict['expert_2022']['Price_REG1']=c
            elif year == 26304 and s == 2:
                dff_dict['expert_2022']['Price_REG2']=c
            elif year == 26304 and s == 3:
                dff_dict['expert_2022']['Price_REG3']=c
            elif year == 26304 and s == 4:
                dff_dict['expert_2022']['Price_REG4']=c

我尝试使用dff_dict['expert_2021']['Price_REG1'].append(c)到 append 行但没有成功,我也尝试了concat但无法让它工作。

我现在有点卡住了,希望得到帮助。

谢谢。

编辑:

在评论之后,输入如下所示:

    Price_REG4
19       31.27
20       30.07
21       29.86
22       29.32
23       28.42

和预期的 output 类似:

       Price_REG1  Price_REG2  Price_REG3  Price_REG4
35059       15.08       15.08       15.08       15.08
35060       11.57       11.57       11.57       11.57
35061       14.89       14.89       14.89       14.89
35062        9.94        9.94        9.94        9.94
35063        4.84        4.84        4.84        4.84

在 2 个单独的数据框中。

您正在使用的数据结构不是很好。 除非你有充分的理由


exp21={'Price_REG1':[],'Price_REG2':[],'Price_REG3':[],'Price_REG4':[]}
exp22={'Price_REG1':[],'Price_REG2':[],'Price_REG3':[],'Price_REG4':[]}

dff_dict={'expert_2021':pd.DataFrame(),'expert_2022':pd.DataFrame()}
for i in year:
    for j in days:
        for column in range(1,5):
            if year == 17544 and s == 1:
                exp21['Price_REG1'].append(c)
            elif year == 17544 and s == 2:
                exp21['Price_REG2'].append(c)
            elif year == 17544 and s == 3:
                exp21['Price_REG3'].append(c)
            elif year == 17544 and s == 4:
                exp21['Price_REG4'].append(c)
            elif year == 26304 and s == 1:
                exp22['Price_REG1'].append(c)
            elif year == 26304 and s == 2:
                exp22['Price_REG2'].append(c)
            elif year == 26304 and s == 3:
                exp22['Price_REG3'].append(c)
            elif year == 26304 and s == 4:
                exp22['Price_REG4'].append(c)
for i in exp21:
    dff_dict['expert_2021'][i]=exp21[i]
    dff_dict['expert_2022'][i]=exp22[i]
    

暂无
暂无

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

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