Python Forum

Full Version: x.sum() VS sum(x) - Calling a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
I legitimately do not see what the difference is between your two examples.
I had copied the same text twice. I have changed it now. The title of the thread points to the difference.