Consider a data frame with 97 rows and 44 columns where i have three columns whose names are “Bostwick”,”mu_yield” , so i’m trying to create a new column called “Target” where if the “Bostwick” column values lie between “5.00 and 6.75” else if “mu_yield” column values lie between “89.00 and 90.00” , the “Target” column values should be 0 else it is 1
I tried the below way
JavaScript
x
3
1
bos['Target'] = np.where(((bos['mu_yield'] < 5.000) | (bos['mu_yield'] > 6.750)), 0,
2
np.where((bos['mu_yield'] < 89.00) | (bos['mu_yield'] > 90.00), 0, 1)))
3
There were no errors but the entire “Target” column values are 0
Hence i tried the below method
JavaScript
1
2
1
bos['Target'] = np.where((bos['Bostwick'] < 5.000) | (bos['Bostwick'] > 6.750)) or ((bos['mu_yield'] < 89.00) | (bos['mu_yield'] > 90.00), 0, 1)
2
Here i’m facing the below value error
JavaScript
1
35
35
1
---------------------------------------------------------------------------
2
ValueError Traceback (most recent call last)
3
~AppDataLocalTemp/ipykernel_35620/4282921525.py in <module>
4
----> 1 bos['Target'] = np.where((bos['Bostwick'] < 5.000) | (bos['Bostwick'] > 6.750)) or ((bos['mu_yield'] < 89.00) | (bos['mu_yield'] > 90.00), 0, 1)
5
6
~anaconda3libsite-packagespandascoreframe.py in __setitem__(self, key, value)
7
3610 else:
8
3611 # set column
9
-> 3612 self._set_item(key, value)
10
3613
11
3614 def _setitem_slice(self, key: slice, value):
12
13
~anaconda3libsite-packagespandascoreframe.py in _set_item(self, key, value)
14
3782 ensure homogeneity.
15
3783 """
16
-> 3784 value = self._sanitize_column(value)
17
3785
18
3786 if (
19
20
~anaconda3libsite-packagespandascoreframe.py in _sanitize_column(self, value)
21
4507
22
4508 if is_list_like(value):
23
-> 4509 com.require_length_match(value, self.index)
24
4510 return sanitize_array(value, self.index, copy=True, allow_2d=True)
25
4511
26
27
~anaconda3libsite-packagespandascorecommon.py in require_length_match(data, index)
28
529 """
29
530 if len(data) != len(index):
30
--> 531 raise ValueError(
31
532 "Length of values "
32
533 f"({len(data)}) "
33
34
ValueError: Length of values (1) does not match length of index (94)
35
Requesting someone to help me on the same
Advertisement
Answer
Use |
for bitwise OR and in original use &
for bitwise AND
:
JavaScript
1
3
1
bos['Target'] = np.where(((bos['Bostwick'] > 5.000) & (bos['Bostwick'] < 6.750)) |
2
((bos['mu_yield'] > 89.00) & (bos['mu_yield'] < 90.00)), 0, 1)
3
Alternative with Series.between
:
JavaScript
1
3
1
bos['Target1'] = np.where(bos['Bostwick'].between(5.000, 6.750, inclusive=False) |
2
bos['mu_yield'].between(89.000, 90.00, inclusive=False), 0, 1)
3