0

I want to create an array with a format, and the values originate from another array. My input array consists out of three columns. I want to create an array with in the first row all values from the third column if the second column is equal. So in this example the first three values in the second column are equal, so in the new array i want the third value of each row in the new array.

a = 
[[1,  1,  4],
[2,  1,  6],
[3,  1,  7],
[4,  2,  0],
[5,  2,  7],
[6,  3,  1]]

result:

b = 
[[4, 6 , 7],
[0, 7],
[1]]

I tried:

c = []    
x = 1
for row in a:
    if row[0] == x
        c.extend[row[2]]
    else:
        x = x + 1
        c.append(row[2])

But the result is a list of all 3rd values

2
  • 1
    are the values in the second line increasing and integer? Commented Aug 16, 2017 at 14:41
  • Jep, they are all integer, and the values in the second column will only increase. However, the amount of values that are the same will vary Commented Aug 16, 2017 at 14:43

3 Answers 3

1
a = np.asarray(a)
c = []
for i in range(a[-1,1]): #a[-1,1] is the maximum that will occur
    save = a[a[:,1]==i] # take all the ones that have i in the second entry
    c.append(save[:,2]) # of those add the last entry

It's important, that ais converted to a np.array for this.

Sign up to request clarification or add additional context in comments.

Comments

1

If the second column is sorted, you can use np.diff to find out the index where the value changes and then split on it:

np.split(a[:,2], np.flatnonzero(np.diff(a[:,1]) != 0)+1)
# [array([4, 6, 7]), array([0, 7]), array([1])]

3 Comments

This results in a list of arrays, is it also possible to create an array immediately?
You can wrap the result in np.array(...) to get an array, but Since the result arrays have different lengths, it will be an array of objects. Normal useful numpy array operations won't apply here.
I would avoid wrapping it into an np.array as it will not behave as usual arrays. In case you really need it I would recommend creating a array with np.nan and then filling in your values, this will use to much information, but will be usable like a normal array.
1

The below works for me:

import numpy as np

c = [[]]    
x = 1
for row in a:
    if row[1] == x:
        c[-1].append(row[2])
    else:
        x = x + 1
        c.append([row[2]])

c = np.asarray(c)

4 Comments

The format is correct, but this results in a list consiting of lists, but because I need to do calculations with the result i prefer an array.
you can add import numpy as np and c = np.asarray(c)
This creates an object array. If i try to open it to preview i get the error object arrays are currently not supported (Spyder). My other arrays are the type float 64 and won't show this error..
I guess as @Psidom writes, the three arrays are unequal length. It's not possible to have one (float 64) array that contains the information presented in the three of them. So either you look at each array separately or go with the object array (array of three arrays), but that can't be displayed in Spyder.

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.