我的 python 应用程序中遇到这个问题。我需要将名为“ValueOverlap”的最新列添加到我的数据框中,其中包含多个条件和计算结果。
日期 | 打开 | 高 | 低 | 关闭 | 前高 | 上一页低 | 方向 | 上一个方向 | 价值重叠 |
---|---|---|---|---|---|---|---|---|---|
1995年12月31日 | 0,2879 | 0,3170 | 0,1429 | 0,1864 | 0,4475 | 0,2824 | 看跌 | 看跌 | 38 |
1996年12月31日 | 0,1886 | 0,2640 | 0,1138 | 0,1172 | 0,3170 | 0,1429 | 看跌 | 看跌 | 13 |
1997年12月31日 | 0,1217 | 0,3906 | 0,1205 | 0,3655 | 0,2640 | 0,1138 | 看涨 | 看跌 | 38 |
1998年12月31日 | 0,3761 | 1,0536 | 0,2857 | 0,9180 | 0,3906 | 0,1205 | 看涨 | 看涨 | 47 |
1999年12月31日 | 0,9364 | 1,3426 | 0,2433 | 0,2656 | 1,0536 | 0,2857 | 看跌 | 看涨 | 34 |
2000年12月31日 | 0,2656 | 0,4843 | 0,2578 | 0,3911 | 1,3426 | 0,2433 | 看涨 | 看跌 | 47 |
2001年12月31日 | 0,3938 | 0,4673 | 0,2386 | 0,2559 | 0,4843 | 0,2578 | 看跌 | 看涨 | 46 |
2002年12月31日 | 0,2564 | 0,4466 | 0,2271 | 0,3816 | 0,4673 | 0,2386 | 看涨 | 看跌 | 32 |
2003年12月31日 | 0,3848 | 1,2423 | 0,3782 | 1,1500 | 0,4466 | 0,2271 | 看涨 | 看涨 | 45 |
2004年12月31日 | 1,1568 | 2,6950 | 1,1179 | 2,5675 | 1,2423 | 0,3782 | 看涨 | 看涨 | 37 |
2005年12月31日 | 2,5850 | 3,3271 | 1,7914 | 3,0300 | 2,6950 | 1,1179 | 看涨 | 看涨 | 46 |
2006年12月31日 | 3,0818 | 7,2486 | 2,9250 | 7,0743 | 3,3271 | 1,7914 | 看涨 | 看涨 | 32 |
2007年12月31日 | 7,1168 | 7,1521 | 2,8264 | 3,0482 | 7,2486 | 2,9250 | 看跌 | 看涨 | 30 |
2008年12月31日 | 3,0671 | 7,6411 | 2,7929 | 7,5261 | 7,1521 | 2,8264 | 看涨 | 看跌 | 36 |
2009年12月31日 | 7,6225 | 11,6664 | 6,7946 | 11,5200 | 7,6411 | 2,7929 | 看涨 | 看涨 | 16 |
2010年12月31日 | 11,6300 | 15,2393 | 11,0893 | 14,4643 | 11,6664 | 6,7946 | 看涨 | 看涨 | 30 |
2011年12月31日 | 14,6214 | 25,1811 | 14,6071 | 19,0061 | 15,2393 | 11,0893 | 看涨 | 看涨 | 48 |
2012年12月31日 | 19,7793 | 20,5407 | 13,7536 | 20,0364 | 25,1811 | 14,6071 | 看涨 | 看涨 | 48 |
2013年12月31日 | 19,8457 | 29,9375 | 17,6268 | 27,5950 | 20,5407 | 13,7536 | 看涨 | 看涨 | 16 |
2014年12月31日 | 27,8475 | 33,6350 | 23,0000 | 26,3150 | 29,9375 | 17,6268 | 看跌 | 看涨 | 28 |
2015年12月31日 | 25,6525 | 29,6725 | 22,3675 | 28,9550 | 33,6350 | 23,0000 | 看涨 | 看跌 | 44 |
2016年12月31日 | 28,9500 | 44,3000 | 28,6900 | 42,3075 | 29,6725 | 22,3675 | 看涨 | 看涨 | 20 |
2017年12月31日 | 42,5400 | 58,3675 | 36,6475 | 39,4350 | 44,3000 | 28,6900 | 看跌 | 看涨 | 37 |
2018年12月31日 | 38,7225 | 73,4925 | 35,5000 | 73,4125 | 58,3675 | 36,6475 | 看涨 | 看跌 | 47 |
2019/12/31 | 74,0600 | 138,7900 | 53,1525 | 132,6900 | 73,4925 | 35,5000 | 看涨 | 看涨 | 16 |
2020年12月31日 | 133,5200 | 182,1300 | 116,2100 | 177,5700 | 138,7900 | 53,1525 | 看涨 | 看涨 | 16 |
2021 年 12 月 31 日 | 177,8300 | 182,9400 | 125,8700 | 129,9300 | 182,1300 | 116,2100 | 看跌 | 看涨 | 34 |
2022 年 12 月 31 日 | 130,2800 | 199,6200 | 124,1700 | 193,6000 | 182,9400 | 125,8700 | 看涨 | 看跌 | 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()."
有办法解决这个问题吗?提前非常感谢!
您不能将
min
与值列表一起使用,您必须使用矢量化 min 函数:
v1 = (df['PrevHigh'] - df['Low']) / (df['High'] - df['Low']) * 100
v2 = (df['PrevHigh'] - df['Low']) / (df['PrevHigh'] - df['PrevLow']) * 100
bullish = pd.DataFrame([v1, v2]).min()
v1 = (df['High'] - df['PrevLow']) / (df['High'] - df['Low']) * 100
v2 = (df['High'] - df['PrevLow']) / (df['PrevHigh'] - df['PrevLow']) * 100
bearish = pd.DataFrame([v1, v2]).min()
df.loc[(df['PrevDirection'] == 'Bullish') & (df['Direction'] == 'Bullish'), 'Value_Overlap'] = bullish
df.loc[(df['PrevDirection'] == 'Bearish') & (df['Direction'] == 'Bearish'), 'Value_Overlap'] = bearish