What does np.where return with one argument?
Why does np.where return a tuple?
How do you use multiple conditions with np.where?
I assume your array looks like:
|(HUE)(VALUE)(CLASS)
row/col| 0 1 2
-------+-----------------
0 | 0 1 2
1 | 3 4 5
2 | 6 7 8
. | . . .
. | . . .
3599999| . . .
And here is the sample code. For simplicity I changed the size 3600000 to 5.
a = np.array(xrange(5 * 3))
a.shape = (5, 3)
Now array a look like this:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
If you want row with HUE=9, do like this:
a[np.where(a[:,0] == 9)]
#array([[ 9, 10, 11]])
If you want row with VALUE=4, do like this:
a[np.where(a[:,1] == 4)]
#array([[3, 4, 5]])
If you want row with HUE=0 and VALUE=1, do like this:
a[np.where((a[:,0] == 0) * (a[:,1] == 1))]
#array([[0, 1, 2]])
Try this code:
x[x[:, 2] == class_number[:, :2]
where x is np.ndarray
x[:, 2] == class_number
contains true/false that means whether the last is class_number or not.
You need to take a look at: Boolean indexing in http://wiki.scipy.org/Cookbook/Indexing
Moved from comment.
IIUC isin with str slice
np.where(df.period.str[:3].isin(l),'month',df.period.str.split(' ').str[0])
Out[1162]: array(['month', 'YTD'], dtype=object)
df['period_type'] = np.where(df.period.str[:3].isin(l),'month',df.period.str.split(' ').str[0])
One way is using str.startswith to check which rows do start with any of the values in the list (it also accepts a tuple of strings), and np.where to set the rows in the new column to month or the actual row value splitted and taking the first value:
l = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
m = df.period.str.startswith(tuple(l))
df['period_type'] = np.where(m, 'month', df.period.str.split().str[0])
df.loc[~m, 'period'] = df.loc[~m, 'period'].str.split().str[1]
period period_type
0 JAN16 month
1 JAN16 YTD
Syntax -
np.where(condition, value1, value2)
Solution -
np.where(
df.productView < df.order, df.productView + df.order, df.productView
)
array([9.5, 4. , 4. , 5. ])
As an efficient alternative, you can use loc:
m = df.productView < df.order
df.loc[m, 'productView'] = df.loc[m, ['productView', 'order']].sum(1)
Maybe mask
df['New']=df.productView.mask(df.order>df.productView,df.sum(1))
df
Out[271]:
productView order New
userId
A 4.5 5.0 9.5
B 1.5 2.5 4.0
C 4.0 2.0 4.0
D 2.0 3.0 5.0