Home > OS >  Python sum() error for converting SQL query to Pandas
Python sum() error for converting SQL query to Pandas

Time:06-16

I am trying to convert a SQL query to equivalent python pandas. My data for SQL is

x=(2.2,2.2,2.2,2.2,2.2)
y=(3.3,3.3,3.3,3.3,3.3)
z=(4.4,4.4,4.4,4.4,4.4)
r=(5.5,5.5,5.5,5.5,5.5)
t=(6.6,6.6,6.6,6.6,6.6)

and the SQL query is:

select sum(sum(x),sum(y),sum(z),
            sum(r),sum(t)) into :m
        from _fx_ght;

I have created Python code to convert the SQL query into pandas:

 x=(2.2,2.2,2.2,2.2,2.2)
    y=(3.3,3.3,3.3,3.3,3.3)
    z=(4.4,4.4,4.4,4.4,4.4)
    r=(5.5,5.5,5.5,5.5,5.5)
    t=(6.6,6.6,6.6,6.6,6.6)

m=sum(sum(x),sum(y),sum(z),sum(r),sum(t))

print(m)

While executing the code I am getting an error which shows:

Traceback (most recent call last):
  File "./prog.py", line 9, in <module>
TypeError: sum() takes at most 2 arguments (5 given)

How can I covert this to the equivalent pandas?

CodePudding user response:

if i understand your question you want to get total sum of each sum x,y,z,r,t?
if yes then

m=sum((sum(x),sum(y),sum(z),sum(r),sum(t)))  

CodePudding user response:

Try:

m=sum([sum(x),sum(y),sum(z),sum(r),sum(t)])

sum() iterates all elements in list and sum.

CodePudding user response:

hmm sum does not work like that you have to put the individual sums into another array so for example try:

sum_tot = None
sum_arr_list = [x,y,z,r,t]

def sum_arr(array):
  return sum(array)
for item in sum_arr_list:
  sum_tot  = sum_arr(item)
m = sum_tot

There are better ways to do this Or Like below put it in a tuple within sum

m = sum((sum(x), ..))

CodePudding user response:

Here is the equvalent pandas

import pandas as pd

df=pd.DataFrame({"x":[2.2,2.2,2.2,2.2,2.2],
                    "y":[3.3,3.3,3.3,3.3,3.3],
                    "z":[4.4,4.4,4.4,4.4,4.4],
                    "r":[5.5,5.5,5.5,5.5,5.5],
                    "t":[6.6,6.6,6.6,6.6,6.6]})


m=df.sum().sum()


  • Related