我在我的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()."

有什么办法来解决这个问题吗?提前谢谢你了!

推荐答案

不能将min与值列表一起使用,必须使用矢量化的MIN函数:

import numpy as np

v1 = (df['PrevHigh'] - df['Low']) / (df['High'] - df['Low']) * 100
v2 = (df['PrevHigh'] - df['Low']) / (df['PrevHigh'] - df['PrevLow']) * 100
bullish = np.min([v1, v2], axis=0)

v1 = (df['High'] - df['PrevLow']) / (df['High'] - df['Low']) * 100
v2 = (df['High'] - df['PrevLow']) / (df['PrevHigh'] - df['PrevLow']) * 100
bearish = np.min([v1, v2], axis=0)

m = (df['PrevDirection'] == 'Bullish') & (df['Direction'] == 'Bullish')
df.loc[m, 'Value_Overlap'] = bullish[m]

m = (df['PrevDirection'] == 'Bearish') & (df['Direction'] == 'Bearish')
df.loc[m, 'Value_Overlap'] = bearish[m]

我得到了一个结果,但Value_Overlap显然不等于ValueOverlap:

>>> df.loc[:, 'Direction':]
   Direction PrevDirection  ValueOverlap  Value_Overlap
0    Bearish       Bearish            38    -151.847365
1    Bearish       Bearish            13     104.766187
2    Bullish       Bearish            38            NaN
3    Bullish       Bullish            47      13.660633
4    Bearish       Bullish            34            NaN
5    Bullish       Bearish            47            NaN
6    Bearish       Bullish            46            NaN
7    Bullish       Bearish            32            NaN
8    Bullish       Bullish            45       7.915750
9    Bullish       Bullish            37     -14.662895
10   Bullish       Bullish            46     -99.101387
11   Bullish       Bullish            32      43.625020
12   Bearish       Bullish            30            NaN
13   Bullish       Bearish            36            NaN
14   Bullish       Bullish            16      17.375508
15   Bullish       Bullish            30      11.845724
16   Bullish       Bullish            48       5.978816
17   Bullish       Bullish            48     108.071685
18   Bullish       Bullish            16      23.669653
19   Bearish       Bullish            28            NaN
20   Bullish       Bearish            44            NaN
21   Bullish       Bullish            20  -12112.778236
22   Bearish       Bullish            37            NaN
23   Bullish       Bearish            47            NaN
24   Bullish       Bullish            16     -39.293262
25   Bullish       Bullish            16      -0.436205
26   Bearish       Bullish            34            NaN
27   Bullish       Bearish            21            NaN

Python相关问答推荐

Pandas使用过滤器映射多列

Python中使用Delivercio进行多个请求

Docker-compose:为不同项目创建相同的容器

将numpy矩阵映射到字符串矩阵

Python panda拆分列保持连续多行

在上下文管理器中更改异常类型

如何计算列表列行之间的公共元素

Locust请求中的Python和参数

使用SciPy进行曲线匹配未能给出正确的匹配

在Google Colab中设置Llama-2出现问题-加载判断点碎片时Cell-run失败

运行总计基于多列pandas的分组和总和

用合并列替换现有列并重命名

将输入聚合到统一词典中

如何在图中标记平均点?

Plotly Dash Creating Interactive Graph下拉列表

如何在FastAPI中为我上传的json文件提供索引ID?

如何检测鼠标/键盘的空闲时间,而不是其他输入设备?

合并与拼接并举

基于多个数组的多个条件将值添加到numpy数组

如何将数据帧中的timedelta转换为datetime