How to subtract the squareroot of every value in the single column of an array?

Question:

I have a 2D array, and I need to subtract the square root of each value from the 2nd column.

So I need to turn this:

array([[ 0,  9],
       [ 2,  16],
       [ 4,  25],
       [10, 36]])

into:

array([[ 0,  6],
       [ 2,  12],
       [ 4,  20],
       [10, 30]])

What I have so far is:

B[:, 1] -= np.sqrt(B[:, 1])

Asked By: D Rob

||

Answers:

does it have to be in numpy?
I like pandas for this:

import pandas as pd

df = pd.DataFrame([[ 2,  16],
[ 4,  25],
[10, 36]])

df[1] = df[1]-df[1]**.5

print(df.to_numpy())

output

[[ 2. 12.]
 [ 4. 20.]
 [10. 30.]]
Answered By: 1extralime

How about

arr = np.array([ [0, 9],
                 [2,  16], 
                 [4,  25],
                 [10, 36]])

arr[:,1] = arr[:,1] - np.sqrt(arr[:,1])

>arr
array([[ 0,  6],
       [ 2, 12],
       [ 4, 20],
       [10, 30]])
Answered By: 7shoe
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.