0

I have created a script for calculating the sum of all the values in 32th column which has comma as delimiter. My script is printing the values but unable to sum the values. What I am doing wrong? Below is my script:

import numpy as np
b=np.loadtxt(r'FileP3806520150316142845.txt',dtype=str,delimiter=',',skiprows=0,usecols=(31,))
print b
a = b[1].sum(0)
print a

I can print variable b, but not a.

python myreport.sh 
['1' '438' '18' '987' '1472' '95' '52' '2' '22' '137' '7' '22']
Traceback (most recent call last):
File "myreport.sh", line 4, in <module>
a = b[1].sum(0)
TypeError: cannot perform reduce with flexible type

Also I am just running this script on one file. Can this be applied to all files in a folder or all files in a tar file?

user2922822
  • 123
  • 1
  • 1
  • 8

1 Answers1

3

Try setting dtype=int. Numpy doesn't know what to do with a bunch of strings.

You can also convert b after the fact:

b_ints = np.int_(b)
André Laszlo
  • 15,169
  • 3
  • 63
  • 81