Python Forum
For Loop Works Fine But Append For Pandas Doesn't Work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Loop Works Fine But Append For Pandas Doesn't Work
#2
I am confused about your confusion. The code works exactly as I would expect. Here is an even simpler example that demonstrates the behavior.
numbers = []
for number in range(5):
    print(number)
    numbers.append(number)
    print(numbers)
Output:
0 [0] 1 [0, 1] 2 [0, 1, 2] 3 [0, 1, 2, 3] 4 [0, 1, 2, 3, 4]
Since a number is appended to numbers each time through the loop I would expect numbers to be longer each time it is printed. This is just like your example where you nprofit to net_profits.

Do you only want to print net_profits after all the values are added?
numbers = []
for number in range(5):
    print(number)
    numbers.append(number)
print(numbers)
Output:
0 1 2 3 4 [0, 1, 2, 3, 4]
Reply


Messages In This Thread
RE: For Loop Works Fine But Append For Pandas Doesn't Work - by deanhystad - Dec-17-2021, 08:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PIP doesn't work YKR 1 711 Mar-28-2025, 02:10 PM
Last Post: snippsat
  I'm trying to install python 3.11.11 on windows 10 - it doesn't work Petonique 2 2,047 Feb-04-2025, 05:42 PM
Last Post: snippsat
  Pandas - error when running Pycharm, but works on cmd line zxcv101 2 2,472 Sep-09-2024, 08:03 AM
Last Post: pinkang
  Questions about while loop - why does one work, and the other variant does not Swgeek 2 889 Aug-29-2024, 07:57 AM
Last Post: Swgeek
  Extending list doesn't work as expected mmhmjanssen 2 1,431 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,963 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 1,440 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  While Loop Does Not Work Properly mactron 4 1,974 Jun-22-2023, 01:04 AM
Last Post: mactron
  Why doesn't this code work? What is wrong with path? Melcu54 7 3,588 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 2,790 Jan-13-2023, 05:09 PM
Last Post: muzicman0

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020