Quazi Nizam Quazi Nizam - 3 months ago 9
Python Question

How to get array after adding a number to specific data of that array?

import numpy as np
import xlrd
import xlwt

wb = xlrd.open_workbook('Scatter plot.xlsx')
workbook = xlwt.Workbook()

sheet = workbook.add_sheet("Sheet1")

sh1 = wb.sheet_by_name('T180')

x= np.array([sh1.col_values(1, start_rowx=51, end_rowx=315)])
y= np.array([sh1.col_values(2, start_rowx=51, end_rowx=315)])

x1= np.array([sh2.col_values(1, start_rowx=50, end_rowx=298)])
y1= np.array([sh2.col_values(2, start_rowx=50, end_rowx=298)])

condition = [(x1<=1000) & (x1>=0) ]
condition1 = [(y1<=1000) & (y1>=0) ]

y_prime= y[condition1]+20

plt.plot(x,y, "ro", label="T180")

I want to subtract 150 from the values less than 1000 of x1 array and finally I need all values (subtracted+remaining). But with this code I got only the values that are less than 1000. But I need both (less than 1000+ greater than 1000). But greater than 1000 values will be unchanged. How can I will do this. As you can see there 248 elements in x1 array so after subtraction I will need 248 element as x_prime. Same as for y. Thanks in advance for your kind co-operation.


You can use numpy.place to modify arrays where a logic expression holds. For complex logic expressions on the array there are the logic functions that combines boolean arrays.


A = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
np.place(A, np.logical_and(A > 1, A <= 8), A-10)

will subtract 10 from every element of A that is > 1 and <= 8. After this A will be

array([ 1, -9, -8, -7, -6, -5, -4, -3,  9, 10])