I hae a list of numpy arrays and want to xport some rows of each array. My list is:
all_data=[np.array([[1., 1., 2.], [2., 1., 4.], [3., 1., 5.], [1., 2., 4.]]),\
np.array([[3., 1., 3.], [4., 1., 4.], [3., 2., 7.], [4., 2., 1.]]),\
np.array([[0., 0., 0.], [1., 1., 1.], [3., 1., 0.], [4., 2., 4.]]),\
np.array([[2., 2., 2.], [3., 2., 1.], [4., 2., 5.], [5., 2., 4.]])]
My all_data has four arrays here (in reality it has much more). In each array I am firstly interested in knowing how many unique or repeated values are existing in the second column. Than, for even array, I wan to extract the last row of the rows having the same value in their second column. If a row has a unique value, I only extract that row. For odd arrays, I want to extrat the first row of the rows having the same value in their second column and again for rows with unique values, I extract that single row. Finally, I want the following list of array and my output:
[np.array([[3., 1., 5.], [1., 2., 4.]]),\
np.array([[3., 1., 3.], [3., 2., 7.]]),\
np.array([[0., 0., 0.], [3., 1., 0.], [4., 2., 4.]]),\
np.array([[2., 2., 2.]])]
I tried the following code but it was not successful at all.
extracted=[]
for i,h in enumerate (all_data):
nums, counts=np.unique(h[:,1],return_counts=True) # to find the frequency of values in second column
if i%2 ==0:
a=np.where(np.isin(h[:,1],nums).any(-1))[-1]
else:
a=np.where(np.isin(h[:,1],nums).any(-1))[0]
extracted.append (a)
I do appreciate any help in advance.