假设我有一个名为df_trading_pair的df,它包含以下数据:

           Start Date  Open Price  High Price  Low Price  Close Price   Volume                End Date
0 2022-08-06 05:30:00      0.3738      0.3741     0.3737       0.3739  13767.0 2022-08-06 05:32:59.999
1 2022-08-06 05:33:00      0.3739      0.3742     0.3738       0.3741  28212.0 2022-08-06 05:35:59.999
2 2022-08-06 05:36:00      0.3740      0.3743     0.3739       0.3740  47274.0 2022-08-06 05:38:59.999
3 2022-08-06 05:39:00      0.3740      0.3740     0.3737       0.3739  55859.0 2022-08-06 05:41:59.999

在跑完df_trading_pair["Volume"]后,你会得到:

0    13767.0
1    28212.0
2    47274.0
3    55859.0
Name: Volume, dtype: float64

如何才能知道后续的每个值是否都大于df_trading_pair["Volume"]中前面的值

最初,我想要编写这样的代码:

if df_trading_pair["Volume"][3] > df_trading_pair["Volume"][2] > df_trading_pair["Volume"][1] > f_trading_pair["Volume"][0]:

   print(True)

但这看起来不太像100

所以我来这里是为了学习一种更好的方法来做到这一点.

我能在这里得到一些帮助吗?

推荐答案

使用:

df_trading_pair["Volume"].diff().iloc[1:].gt(0).all()

输出:True

解释:

(df_trading_pair["Volume"]
.diff()     # compute pairwise difference
.iloc[1:]   # remove first row
.gt(0)      # are the differences positive?
.all()      # are ALL differences positive?
)

numpy个替代方案:

a = df_trading_pair["Volume"].to_numpy()

(a[1:]>a[:-1]).all()
# True

Python-3.x相关问答推荐

将数据帧扩展为矩阵索引

使用递归将int转换为字符串

合并两个数据帧并对某些总和进行求和

try 使用 GEKKO 求解非线性方程组.系统有多种解决方案,但 GEKKO 给出了错误的解决方案.我该如何解决?

它们是否同样存储在python3的内存中?

隐藏Cartopy中高纬度非矩形投影的右侧轴(纬度)标签

如何将 OLS 趋势线添加到使用 updatemenus 显示数据子集的 plotly 散点图图形对象?

使用 python 查找标记的元素

Pandas matplotlib:条形图占总数的百分比

如何通过从特定列创建分组多标题来reshape 数据框?

删除Pandas 数据框行不起作用

以编程方式映射 uniprot ID 时如何解决 400 客户端错误?

聚合(aggregate)为最多包含两个元素的列表

python中两个连续的yield语句如何工作?

使用 Python 解析 JSON 嵌套字典

在初始化之前禁用`__setattr__`的干净方法

使用自定义比较删除重复项

如何避免使用我的 python 包构建 C 库?

在 Python 中生成马尔可夫转移矩阵

将 Python 字节转换为无符号 8 位整数