Search code examples
pythonsumiterable

python sum iterable variable


very simple question, I think. I have multiple dataframes, that I'm summing them to obtain a result, ex :

df1 = 272.6692725
df2 = 224.876735
df3 = 262.70736
df4 = 329.1992858333333
df5 = 542.3639916666667

And like it for a long serie. I just want to sum all this in another variable, like "total = ..."

So I tried something like :

for x in range(0,300):
    x +=1
    total = sum(globals()[f"df{x}"])

But I have "TypeError: 'numpy.float64' object is not iterable". I think I don't have a good approach but can't figure it out.


Solution

  • If you want to sum for example always that x value to the total you can just do

    for x in range(300): #Dont need to mention 0 
        total += x # Your x is already being incremented
    

    If that is not what you want, I am sorry. It was what I interpreted