How to Store the for Statement Variable

Asked 1 years ago, Updated 1 years ago, 321 views

The for statement overwrites the looped content for each purchase order number, and
When stored as a variable, only the last action was stored.
There is no problem that I can see the looped contents when I display it in print. Please tell me how to write the code to store all looped contents in the group_list variable.

for i, group training_data.groupby('Order Number'):
    print(group)
    group_list=group.append(group, ignore_index=True)
group_list

python

2023-02-27 15:12

1 Answers

As you can see in the comments, I don't think you need to go out of your way to turn the for statement, but I would like to point out the reason for the unexpected behavior.

The for statement overwrites the looped content for each purchase order number, and when stored as a variable, only the last action was stored.

Because it is group.append(group), it is in the state that ignores all previous data and overwrites the group_list with two lines of the same data.
If you want to merge, it must be group_list.append(group).

for i, group training_data.groupby('Order Number'):
    print(group)
    group_list=group_list.append(group, ignore_index=True)
group_list


2023-02-27 15:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.