我有以下数据框:如何在lambda中执行多个值的操作?
In: df
AAPL IBM
2016-04-27 00:00:00+00:00 98.113 NaN
2016-04-28 00:00:00+00:00 97.288 NaN
2016-04-29 00:00:00+00:00 94.147 145.932
2016-05-02 00:00:00+00:00 93.511 145.595
2016-05-03 00:00:00+00:00 95.161 143.515
2016-05-04 00:00:00+00:00 95.320 143.614
2016-05-05 00:00:00+00:00 94.070 145.892
2016-05-06 00:00:00+00:00 93.450 147.970
2016-05-09 00:00:00+00:00 93.770 148.200
2016-05-10 00:00:00+00:00 93.570 150.040
我回到2个最大值从另一个数据框(bar
),这些都是:
In: max_values = bar.max()
In: max_values
Out: AAPL 111.710
IBM 151.676
我想要得到的值,列名,如果可能的话它的索引这些特定值的结果值在0.04和0.08之间。并将它们存储在新的数据框中。我试过这个:
foo = df.apply((lambda s:(0.04 <=(1 - s.max()/max_values) <= 0.08)==True))
但是由于max_values
包含两个值而出现错误。
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index AAPL')
如何在每个值max_values
在lambda
进行操作?
我想要的输出是这样的:
In: foo
Out: AAPL 93.511
IBM 145.892
#this means 93.511 and 145.892 are the values, upon which performing percentage operation,
returned value which was between 0.04 and 0.08.
你可能想'abs(1 - s.max()/ max_values)',不是?否则它不是对称的 – Jacquot