Python Forum
x.sum() VS sum(x) - Calling a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
x.sum() VS sum(x) - Calling a function
#1
I think there's something rather fundamental I have missed regarding calling functions.

I need to fill a column in one dataframe with the sum of the columns in another dataframe.

First I did something like:
for x in range(1,genericreturnperiods+1):
    currentsum = sum(genericresults['%ddaylog' %x])
So for each loop i add the sum of another column. This returned 'NaN' for all columns including at least one NaN value.
Then instead I did:

for x in range(1,genericreturnperiods+1):
    currentsum = genericresults['%ddaylog' %x].sum()
This seems to work just fine, or at least I get a sum instead of just NaN. Why would this be?

BTW: I know 'in range(1,genericreturnperiods+1)' might be non-pythonic, but I need to make it start at 1 not 0 - so 'x in range(genericreturnperiods)' alone wouldn't work.
Reply
#2
I legitimately do not see what the difference is between your two examples.
Reply
#3
I had copied the same text twice. I have changed it now. The title of the thread points to the difference.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  calling external function with arguments Wimpy_Wellington 7 1,426 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 814 Jun-07-2023, 08:55 PM
Last Post: deanhystad
Sad Iterate randint() multiple times when calling a function Jake123 2 2,039 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,810 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  [Solved] TypeError when calling function Laplace12 2 2,876 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  calling a function and argument in an input phillup7 3 2,611 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,239 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Calling DLL function OptoBruh 0 1,589 Nov-15-2019, 11:51 PM
Last Post: OptoBruh
  Duplicate output when calling a custom function from the same file? road2knowledge 2 2,384 May-10-2019, 07:58 AM
Last Post: road2knowledge
  How can I prevent context switching when calling an async function? AlekseyPython 1 2,839 Feb-20-2019, 10:37 AM
Last Post: AlekseyPython

Forum Jump:

User Panel Messages

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