Issue
Hello i have a txt file that i want to make into an array. I want to remove the rows in my array that doesnt fill my conditions. For example Comlumn 0 must be between 10 and 60 Column 1 must be positiv Column 2 must be between 1 and 4
I have tryed finding a way to define the conditions, but whit no luck. I have written the folowing code:
RD = np.loadtxt(filename)
for i in range(len(RD)):
if (RD[:,0] < 10 or RD[:,0] > 60):
RD= np.delete(RD,[i,0])
elif RD[:,2]<0:
RD= np.delete(RD,[i,1])
elif (RD[:,2]<1 or RD[:,2]>4):
RD= np.delete(RD,[i,2])
print(RD)
Can you help me define the conditions correctly?
Solution
You probably want to make a variable which is a set of row indexes which fail the condition, and then pass that variable as the argument through the function np.delete() at the end.
rows_to_del=[row for row in range(RD.shape[0])
if (RD[row,0] < 10 or RD[row,0] > 60)
or RD[row,2] > 0
or (RD[row,2]<1 or RD[row,2]>4)]
RD = np.delete(RD, rows_to_del, axis=0)
Answered By - Michael Green
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.