user3378649
user3378649

Reputation: 5354

How to use sum values of matrix with filter

I am reading file using pandas. d= pandas.DataFrame("data.csv")

data.csv A B C

   d  408.56087701   87.26907024
   b 277.95015117   75.19386881 
   b 385.41416264   84.73488504 
   b 380.31630662   71.23504808 
   b 392.10729207   83.80720357 
   b  399.70877373   76.59640833 
   b 350.93124656   79.34979059 
   b 330.09702335   79.37166555 

back= [399.70877373,385.41416264]

I am trying to sum values of C where I find match between "back" and column B

s=0
for indj, j in enumerate(back)
   for indi, i in enumerate(d) 
      if (j== i):
         s= s+d[indi][3]

I am trying to implement this using reduce :

reduce(lambda x, y: x+y,dat ..)

but i couldn't find a way to add condition to filter values ?

Upvotes: 0

Views: 211

Answers (1)

user3378649
user3378649

Reputation: 5354

I just solved this using

the_sum = sum(x[2] for x in data if x[1] in back)

Upvotes: 1

Related Questions