Reputation: 3880
I have data
diff_time
0
124
56
87
40
168
449
13
I should write a function, where I can get 2 parameters automatically a and b
, where
for el in df['diff_time']:
if el > a:
el = b
else:
el = continue
How can I solve this task? Is it real to do using scipy.optimize?
Upvotes: 2
Views: 74
Reputation: 863301
I think you can use mask
by boolean mask
created by df.diff_time > a
:
a = 100
b = 33
print (df.diff_time > a)
0 False
1 True
2 False
3 False
4 False
5 True
6 True
7 False
Name: diff_time, dtype: bool
df['b'] = df.diff_time.mask(df.diff_time > a, b)
print (df)
diff_time b
0 0 0
1 124 33
2 56 56
3 87 87
4 40 40
5 168 33
6 449 33
7 13 13
If you need change column diff_time
use ix
:
df.ix[df['diff_time']>= a ,'diff_time'] = b
print (df)
diff_time
0 0
1 33
2 56
3 87
4 40
5 33
6 33
7 13
Upvotes: 1
Reputation: 705
a = 100
b = 33
df['diff_time'].apply(lambda el: b if el > a else el)
0 0
1 33
2 56
3 87
4 40
5 33
6 33
7 13
Name: diff_time, dtype: int64
Upvotes: 0