0

I have a data array to which I have inserted a column with values starting from 0. So now my data contains all float values.

After some processing I have a array ARR which contains values from my 1st column. Using these ARR values (or positions) I want to extract the remaining columns to a new array using Numpy.

import numpy as np
import io
data =io.StringIO("""
ID,1,2,3
5362,0.97,-0.40,-0.76
485,-0.65,0.53,0.62
582,0.04,0.99,0.76
99,0.77,0.53,0.39
75,-0.44,0.52,0.85
474,0.35,0.8,0.13
594,-0.29,0.03,0.74
597,0.96,-0.35,0.59
124,0.73,0.61,0.76
635,0.88,0.96,-0.98
7894,-0.04,0.04,0.52
1268,0.56,0.73,-0.37
38,0.46,0.62,0.73
88,0.02,0.16,0.098
887,0.46,0.65,-0.89
""")

data = np.genfromtxt(data, delimiter=',', skip_header=2, dtype=np.float64)
index = np.arange(data.shape[0]).reshape(-1, 1)
data = np.hstack([index, data])


ARR = [[1.],[2.],[4.],[6.],[7.],[9.]]

Expected Output Like:

I want all the row values at the positions indicated in the ARR. I can also show it as Integer values. I want all the rows at these positions in my data array.

485,-0.65,0.53,0.62
582,0.04,0.99,0.76
75,-0.44,0.52,0.85
594,-0.29,0.03,0.74
597,0.96,-0.35,0.59
635,0.88,0.96,-0.98

2 Answers 2

1

You only need to do the following (no need to add an index column to the data). Also, I do not understand why you specify the row numbers as floats (1., 2., 4.,...). Your row numbers must be integers. The solution here corrects this aspect.

# np.set_printoptions(precision=3, suppress=True)
data = np.genfromtxt(data, delimiter=',', skip_header=2, dtype=np.float64)
ARR = [[1.],[2.],[4.],[6.],[7.],[9.]]
ARR = np.array(ARR).astype(int).flatten()
data[ARR, :]

Output:

array([[485.  ,  -0.65,   0.53,   0.62],
       [582.  ,   0.04,   0.99,   0.76],
       [ 75.  ,  -0.44,   0.52,   0.85],
       [594.  ,  -0.29,   0.03,   0.74],
       [597.  ,   0.96,  -0.35,   0.59],
       [635.  ,   0.88,   0.96,  -0.98]])

Optionally:
In case you must add the index, then use the following code block in addition to the code you posted in the question.

ARR = np.array(ARR).astype(int).flatten()
data[ARR, 1:]
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. I needed the locations initially but the array was taking float values, but this solution solved it as I needed the index of the array.
1

Using the way data is shaped, you can easily do:

for value in ARR:
    print(data[int(value[0])][1::])

Hope it helps!

Edit: Added [1::] to slice the result and remove the index position.

2 Comments

If I assign it to a variable new_arr = data[int(value[0])][1::], and try to save it to csv, I am getting different values. If I do not assign, it is working fine. Also the other solution provided works.
I don't know how you're saving to a csv, but you might have to add a newline character between lines as you save them. Regardless, glad you found a working solution!

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.