Reputation: 25
I have a dataframe like this how do I use the return value from np.where
samples= pd.DataFrame({'data':['1','2', '5','6','2','1']})
Output :
data
0 1
1 2
2 6
3 5
4 2
5 1
I can use :
samples['result'] = np.where(samples['data'] > samples['data'].shift(-1), 'High', 'Low')
output :
data result
0 1 Low
1 2 High
2 6 High
3 5 Low
4 2 Low
5 1 Low
I want create a new column and get value from data column if the result is high
expect output :
if result == High, create new column and get value from data on that row, or call a function
data result final
0 1 Low NaN
1 2 High 2 if High do somthing with other function
2 6 High 6 else
3 5 Low NaN if Low call other function
4 2 Low NaN
5 1 Low NaN
or if I can call a function insdie the np.where()
a = print('this is High')
b = print('this is Low')
final = np.where(samples['data'] > samples['data'].shift(-1), a, b)
Upvotes: 1
Views: 761
Reputation: 12818
You can do as follows to use a function within np.where()
:
samples = pd.DataFrame({'data':[1, 2, 5, 6, 2, 1]})
samples['result'] = np.where(
samples['data'] > samples['data'].shift(-1),
'High',
'Low',
)
samples['final'] = np.where(
samples['data'] > samples['data'].shift(-1),
samples['data'] * 5,
np.sin(samples['data']),
)
Resulting dataframe:
data result final
0 1 Low 0.841471
1 2 Low 0.909297
2 5 Low -0.958924
3 6 High 30.000000
4 2 High 10.000000
5 1 Low 0.841471
Upvotes: 0
Reputation: 226
apply
with axis=1
can do it:
>>> samples['final'] = samples.agg(lambda row: row['data'] if row['result'] == 'High' else np.nan, axis=1)
>>> samples
data result final
0 1 Low NaN
1 2 Low NaN
2 5 Low NaN
3 6 High 6
4 2 High 2
5 1 Low NaN
You can call other functions inside the lambda expression.
Upvotes: 1