我在我的python应用程序中遇到了这个问题.我需要在我的数据框中添加一个名为"ValueOverlap"的最新列(这个值是我自己编造的),其中包含多个条件和计算结果.
Date | Open | High | Low | Close | PrevHigh | PrevLow | Direction | PrevDirection | ValueOverlap |
---|---|---|---|---|---|---|---|---|---|
31/12/1995 | 0,2879 | 0,3170 | 0,1429 | 0,1864 | 0,4475 | 0,2824 | Bearish | Bearish | 38 |
31/12/1996 | 0,1886 | 0,2640 | 0,1138 | 0,1172 | 0,3170 | 0,1429 | Bearish | Bearish | 13 |
31/12/1997 | 0,1217 | 0,3906 | 0,1205 | 0,3655 | 0,2640 | 0,1138 | Bullish | Bearish | 38 |
31/12/1998 | 0,3761 | 1,0536 | 0,2857 | 0,9180 | 0,3906 | 0,1205 | Bullish | Bullish | 47 |
31/12/1999 | 0,9364 | 1,3426 | 0,2433 | 0,2656 | 1,0536 | 0,2857 | Bearish | Bullish | 34 |
31/12/2000 | 0,2656 | 0,4843 | 0,2578 | 0,3911 | 1,3426 | 0,2433 | Bullish | Bearish | 47 |
31/12/2001 | 0,3938 | 0,4673 | 0,2386 | 0,2559 | 0,4843 | 0,2578 | Bearish | Bullish | 46 |
31/12/2002 | 0,2564 | 0,4466 | 0,2271 | 0,3816 | 0,4673 | 0,2386 | Bullish | Bearish | 32 |
31/12/2003 | 0,3848 | 1,2423 | 0,3782 | 1,1500 | 0,4466 | 0,2271 | Bullish | Bullish | 45 |
31/12/2004 | 1,1568 | 2,6950 | 1,1179 | 2,5675 | 1,2423 | 0,3782 | Bullish | Bullish | 37 |
31/12/2005 | 2,5850 | 3,3271 | 1,7914 | 3,0300 | 2,6950 | 1,1179 | Bullish | Bullish | 46 |
31/12/2006 | 3,0818 | 7,2486 | 2,9250 | 7,0743 | 3,3271 | 1,7914 | Bullish | Bullish | 32 |
31/12/2007 | 7,1168 | 7,1521 | 2,8264 | 3,0482 | 7,2486 | 2,9250 | Bearish | Bullish | 30 |
31/12/2008 | 3,0671 | 7,6411 | 2,7929 | 7,5261 | 7,1521 | 2,8264 | Bullish | Bearish | 36 |
31/12/2009 | 7,6225 | 11,6664 | 6,7946 | 11,5200 | 7,6411 | 2,7929 | Bullish | Bullish | 16 |
31/12/2010 | 11,6300 | 15,2393 | 11,0893 | 14,4643 | 11,6664 | 6,7946 | Bullish | Bullish | 30 |
31/12/2011 | 14,6214 | 25,1811 | 14,6071 | 19,0061 | 15,2393 | 11,0893 | Bullish | Bullish | 48 |
31/12/2012 | 19,7793 | 20,5407 | 13,7536 | 20,0364 | 25,1811 | 14,6071 | Bullish | Bullish | 48 |
31/12/2013 | 19,8457 | 29,9375 | 17,6268 | 27,5950 | 20,5407 | 13,7536 | Bullish | Bullish | 16 |
31/12/2014 | 27,8475 | 33,6350 | 23,0000 | 26,3150 | 29,9375 | 17,6268 | Bearish | Bullish | 28 |
31/12/2015 | 25,6525 | 29,6725 | 22,3675 | 28,9550 | 33,6350 | 23,0000 | Bullish | Bearish | 44 |
31/12/2016 | 28,9500 | 44,3000 | 28,6900 | 42,3075 | 29,6725 | 22,3675 | Bullish | Bullish | 20 |
31/12/2017 | 42,5400 | 58,3675 | 36,6475 | 39,4350 | 44,3000 | 28,6900 | Bearish | Bullish | 37 |
31/12/2018 | 38,7225 | 73,4925 | 35,5000 | 73,4125 | 58,3675 | 36,6475 | Bullish | Bearish | 47 |
31/12/2019 | 74,0600 | 138,7900 | 53,1525 | 132,6900 | 73,4925 | 35,5000 | Bullish | Bullish | 16 |
31/12/2020 | 133,5200 | 182,1300 | 116,2100 | 177,5700 | 138,7900 | 53,1525 | Bullish | Bullish | 16 |
31/12/2021 | 177,8300 | 182,9400 | 125,8700 | 129,9300 | 182,1300 | 116,2100 | Bearish | Bullish | 34 |
31/12/2022 | 130,2800 | 199,6200 | 124,1700 | 193,6000 | 182,9400 | 125,8700 | Bullish | Bearish | 21 |
我使用以下代码测试了df.loc函数:
df.loc[(df['PrevDirection'] == 'Bullish') & (df['Direction'] == 'Bullish'), 'Value_Overlap'] = min((df['PrevHigh'] - df['Low']) / (df['High'] - df['Low']) * 100, (df['PrevHigh'] - df['Low']) / (df['PrevHigh'] - df['PrevLow']) * 100)
df.loc[(df['PrevDirection'] == 'Bearish') & (df['Direction'] == 'Bearish'), 'Value_Overlap'] = min((df['High'] - df['PrevLow']) / (df['High'] - df['Low']) * 100, (df['High'] - df['PrevLow']) / (df['PrevHigh'] - df['PrevLow']) * 100)
并将lambda函数与下面的代码一起使用:
df['Value_Overlap'] = df.apply(lambda x: min((df['PrevHigh'] - df['Low']) / (df['High'] - df['Low']) * 100, (df['PrevHigh'] - df['Low']) / (df['PrevHigh'] - df['PrevLow']) * 100) if x['PrevDirection'] == 'Bullish' and x['Direction'] == 'Bullish' else min((df['High'] - df['PrevLow']) / (df['High'] - df['Low']) * 100, (df['High'] - df['PrevLow']) / (df['PrevHigh'] - df['PrevLow']) * 100) if x['PrevDirection'] == 'Bearish' and x['Direction'] == 'Bearish' else 0, axis=1)
在展位中的代码是相同的错误:"ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."
有什么办法来解决这个问题吗?提前谢谢你了!