嵌套循环中生成的Python

嵌套循环中生成的Python

本文介绍了嵌套循环中生成的Python Append数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序有两个 for 循环.我在每个循环中生成一个df.我想附加这个结果.对于内部循环的每次迭代,将生成1行24列的数据.对于外循环的每次迭代,它将生成8行24列的数据.我在以正确的方式附加内容时遇到了问题,因此最终数据帧具有8行和24列.我的代码:

My program has two for loops. I generate a df in each looping. I want to append this result. For each iteration of inner loop, 1 row and 24 columns data is generated. For each iteration of outer loop, it generates 8 rows 24 columns data. I am having issues in appending in the right way so the final dataframe has 8 rows and 24 columns.My code:

biglist = []
# The actual code is bigger. Below is representation of it.
for i in range (x1,...,x8):
   tem_list = []
   for j in range ([y1,y2,y3],[y4,..]...[y22,y23,y24]):
       tem_df = pd.DataFrame({'y1':[value1],'y2':[value2],'y3':[value3]},index=i)
       tem_list.append(tem_df)
   biglist.append(tem_list)
# convert listss of lists in biglist to a simple list of dfs
biglist1 = [item for sublist in biglist for item in sublist]
df = pd.concat(biglist1)
print(df)

当前输出:

# below is actual output of my dataframe:
      Pmpp_loss Pmpp_delmt  ... Rsh_delmt Rsh_desen
s1    17.0326    42.5349  ...       NaN       NaN
s2        NaN        NaN  ...       NaN       NaN
s3        NaN        NaN  ...       NaN       NaN
s4        NaN        NaN  ...       NaN       NaN
s5        NaN        NaN  ...       NaN       NaN
s6        NaN        NaN  ...       NaN       NaN
s7        NaN        NaN  ...       NaN       NaN
s8        NaN        NaN  ...   92.1853 -0.444959

[8 rows x 192 columns]

在上面,8行是正确的.但是我得到了192列,而不是24列.在这里,24列被重复了8次.这就是我们在这里看到许多NaN的原因.

In the above, 8 rows is correct. But I got 192 columns, instead of 24. Here, 24 columns got repeated 8 times. That is the reason we see many NaNs here.

推荐答案

尝试:

  • 将此 biglist.append(tem_list)更改为此: biglist.append(pd.concat(tem_list)).

删除此行: biglist1 = [biglist中的子列表项,sublist中的项]

将这一个 df = pd.concat(biglist1)修改为 df = pd.concat(biglist)

如果已定义列名,则还可以在循环作用域之外创建一个空的DataFrame,并从内部循环中直接在其上附加数据:

If you have defined column names, you can also create an empty DataFrame outside your looping scope, and append the data directly on it from your inner loop:

# Before loop
colnames = ['y1', 'y2', 'y3']
df = pd.DataFrame(data=None, columns=colnames)

将附加行固定为内部循环中的单个行:

chaging your append lines to a single one inside your inner loop:

df = df.append(tem_df)

不需要使用 biglist tem_list pd.concat .

在用户评论后编辑:

biglist = []
for i in range (x1,...,x8):
    for j in range ([y1,y2,y3],[y4,..]...[y22,y23,y24]):
        tem_df = pd.DataFrame({'y1':[value1],'y2':[value2],'y3':[value3]},index=i)
    biglist.append(pd.concat(tem_df),axis=1)
df = pd.concat(biglist)
print(df)

这篇关于嵌套循环中生成的Python Append数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:51