Skip to content Skip to sidebar Skip to footer

For Loop Is Not Working While Appending Rows

I am trying to loop over my dataframe and looking for additional 3 rows for each element in df.con which is only looping over 2nd elementUS and missing UK. Please find the attached

Solution 1:

Each time through the loop, s is re-initialized to a new dataframe on this line:

s = n_df_2.append(bev_child, ignore_index=True)

This makes s end up as the original value of n_df_2, plus only the three values that are appended to it the last time the loop body is executed.

I think this is closer to what you want (nothing before the loop changes):

for country in df.con.unique():

    bev_child = n_df_2[(n_df_2['con'].str.contains(country)) & (n_df_2['age group'].str.contains(age_list[0]))]
    bev_work = n_df_2[(n_df_2['con'].str.contains(country)) & (n_df_2['age group'].str.contains(age_list[1]))]
    bev_old = n_df_2[(n_df_2['con'].str.contains(country)) & (n_df_2['age group'].str.contains(age_list[2]))]

    bev_child.loc[:, 'population'] = bev_work.loc[:, 'population'].max() / bev_child.loc[:, 'population'].max()
    bev_child.loc[:, 'con'] = country + '-' + new_list[0]
    bev_child.loc[:, 'age group'] = new_list[0]
    n_df_2 = n_df_2.append(bev_child, ignore_index=True)

    bev_child.loc[:, 'population'] = bev_child.loc[:, 'population'].max() + bev_old.loc[:,
                                                                            'population'].max() / bev_work.loc[:,
                                                                                                  'population'].max()
    bev_child.loc[:, 'con'] = country + '-' + new_list[2]
    bev_child.loc[:, 'age group'] = new_list[2]
    n_df_2 = n_df_2.append(bev_child, ignore_index=True)

    bev_child.loc[:, 'population'] = bev_old.loc[:, 'population'].max() / bev_work.loc[:, 'population'].max()
    bev_child.loc[:, 'con'] = country + '-' + new_list[1]
    bev_child.loc[:, 'age group'] = new_list[1]
    n_df_2 = n_df_2.append(bev_child, ignore_index=True)

print(n_df_2)

Output:

yearagegroupconpopulation02019(0-14)UK10.012019(14-50)UK20.022019(50+)UK300.032020(0-14)US400.042020(14-50)US1000.052020(50+)US2000.062019         youngvschildUK-youngvschild2.072019  unemployedvsworkingUK-unemployedvsworking17.082019           oldvsyoungUK-oldvsyoung15.092020         youngvschildUS-youngvschild2.5102020  unemployedvsworkingUS-unemployedvsworking4.5112020           oldvsyoungUS-oldvsyoung2.0

Note that this only loops through the unique values in df.con, so the loop body only runs twice. Three records are added to the output each time the loop runs. Note also that the output is appended to n_df_2, so there's not need for the variable s.

Post a Comment for "For Loop Is Not Working While Appending Rows"