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])
CodePudding user response:
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.]]
CodePudding user response:
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]])