1

I am facing the following issue. I have 4 data files (Data_1...Data_4) and I am trying to get a count of the number of items in column 2 which are less than or equal to 5. My following code does that job.

import numpy as np
filelist=[]
for i in list(range(1,5)):
    filelist.append("/Users/Hrihaan/Desktop/Data_%s.txt" %i)
for fname in filelist:
    data=np.loadtxt(fname)
    z=data[:,1]
    count= len([i for i in z if i <= -5]) # output 5 3 0 9
    x= np.array(count)
    Average=np.mean(x)
    print(Average)

But I am stuck on how to deal with the output (5,3,0,9), I want to create an array from the output (count) to do simple mathematical calculations such as finding mean or median.So the mean should be (5+3+0+9/4 = 4.25) but when I tried the print(average), I am getting the same output 5 3 0 9, not the mean.

Any help would be greatly appreciated.

1
  • looks like you need to accumulate count in a list in your for frname loop then turn that into an array outside/after the loop (remove the indentation) Commented Jan 13, 2018 at 1:18

2 Answers 2

2

Instead of creating an array for every cycle in the loop, first create a list and convert this list into an array after the loop has finished.

import numpy as np
filelist=[]
for i in list(range(1,5)):
    filelist.append("/Users/Hrihaan/Desktop/Data_%s.txt" %i)

counts = []
for fname in filelist:
    data=np.loadtxt(fname)
    z=data[:,1]
    count= len([i for i in z if i <= -5]) # output 5 3 0 9
    counts.append(count)

x = np.array(counts)
Average=np.mean(x)
print(Average)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, I have used this procedure before, trying out for something within the loop.
@Hrihaan I think this is the best answer close to what you want. Your loop only get one count each step, you can't average one value in a loop. you have to gather all the counts first, then do an average operation.
1

I'm not sure I understand what you're asking, and your code can't be reproduced, as we don't have your files.

But if I understand right, you should remove len() from your code :

for fname in filelist:
    #...
    count = [i for i in z if i <= -5]
    count = [5, 3, 0, 9] # for instance
    x = np.array(count) # x will output: array([5, 3, 0, 9])
    Average = np.mean(x) 
    print(Average) # will print: 4.25

2 Comments

Thanks, but cutting down the len() will basically give me all the values from that column which are less than or equal to -5, I just need the count of values that meet that criteria and create the output from the counts of the 4 individual data files.
The important part in my answer is "your code can't be reproduced" ;). Moreover len() will always return an int afaik, so the comment in your code is unclear. @M472's answer might be what you are looking for, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.