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?

1
  • The elements of b are strings, not a numeric type. Commented Mar 16, 2015 at 9:49

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Yes thanks. setting dtype to int worked. Is there a way that I can apply this script to all files in a folder, or all files in a zip?
Many different ways! ☺ Try searching for that here on SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.