数据帧:如何在同一列中的2个相同的数字之间获得最小数字?

发布于 2025-02-13 19:14:08 字数 5997 浏览 1 评论 0原文

绝对容易通过一个示例显示:

{'Close': {Timestamp('1980-12-12 00:00:00'): 28.75, Timestamp('1980-12-15 00:00:00'): 27.25, Timestamp('1980-12-16 00:00:00'): 25.25, Timestamp('1980-12-17 00:00:00'): 25.87, Timestamp('1980-12-18 00:00:00'): 26.63, Timestamp('1980-12-19 00:00:00'): 28.25, Timestamp('1980-12-22 00:00:00'): 29.63, Timestamp('1980-12-23 00:00:00'): 30.88, Timestamp('1980-12-24 00:00:00'): 32.5, Timestamp('1980-12-26 00:00:00'): 35.5, Timestamp('1980-12-29 00:00:00'): 36.0, Timestamp('1980-12-30 00:00:00'): 35.12, Timestamp('1980-12-31 00:00:00'): 34.13, Timestamp('1981-01-02 00:00:00'): 34.5, Timestamp('1981-01-05 00:00:00'): 33.75, Timestamp('1981-01-06 00:00:00'): 32.25, Timestamp('1981-01-07 00:00:00'): 30.88, Timestamp('1981-01-08 00:00:00'): 30.25, Timestamp('1981-01-09 00:00:00'): 31.88, Timestamp('1981-01-12 00:00:00'): 31.62, Timestamp('1981-01-13 00:00:00'): 30.5, Timestamp('1981-01-14 00:00:00'): 30.63, Timestamp('1981-01-15 00:00:00'): 31.25, Timestamp('1981-01-16 00:00:00'): 31.0, Timestamp('1981-01-19 00:00:00'): 32.87, Timestamp('1981-01-20 00:00:00'): 31.88, Timestamp('1981-01-21 00:00:00'): 32.5, Timestamp('1981-01-22 00:00:00'): 32.87, Timestamp('1981-01-23 00:00:00'): 32.75, Timestamp('1981-01-26 00:00:00'): 32.25}, 'Roll_Max': {Timestamp('1980-12-12 00:00:00'): 28.75, Timestamp('1980-12-15 00:00:00'): 28.75, Timestamp('1980-12-16 00:00:00'): 28.75, Timestamp('1980-12-17 00:00:00'): 28.75, Timestamp('1980-12-18 00:00:00'): 28.75, Timestamp('1980-12-19 00:00:00'): 28.75, Timestamp('1980-12-22 00:00:00'): 29.63, Timestamp('1980-12-23 00:00:00'): 30.88, Timestamp('1980-12-24 00:00:00'): 32.5, Timestamp('1980-12-26 00:00:00'): 35.5, Timestamp('1980-12-29 00:00:00'): 36.0, Timestamp('1980-12-30 00:00:00'): 36.0, Timestamp('1980-12-31 00:00:00'): 36.0, Timestamp('1981-01-02 00:00:00'): 36.0, Timestamp('1981-01-05 00:00:00'): 36.0, Timestamp('1981-01-06 00:00:00'): 36.0, Timestamp('1981-01-07 00:00:00'): 36.0, Timestamp('1981-01-08 00:00:00'): 36.0, Timestamp('1981-01-09 00:00:00'): 36.0, Timestamp('1981-01-12 00:00:00'): 36.0, Timestamp('1981-01-13 00:00:00'): 36.0, Timestamp('1981-01-14 00:00:00'): 36.0, Timestamp('1981-01-15 00:00:00'): 36.0, Timestamp('1981-01-16 00:00:00'): 36.0, Timestamp('1981-01-19 00:00:00'): 36.0, Timestamp('1981-01-20 00:00:00'): 36.0, Timestamp('1981-01-21 00:00:00'): 36.0, Timestamp('1981-01-22 00:00:00'): 36.0, Timestamp('1981-01-23 00:00:00'): 36.0, Timestamp('1981-01-26 00:00:00'): 36.0}, 'Daily_Drawdown': {Timestamp('1980-12-12 00:00:00'): 0.0, Timestamp('1980-12-15 00:00:00'): -0.05217391304347829, Timestamp('1980-12-16 00:00:00'): -0.12173913043478257, Timestamp('1980-12-17 00:00:00'): -0.10017391304347822, Timestamp('1980-12-18 00:00:00'): -0.07373913043478264, Timestamp('1980-12-19 00:00:00'): -0.017391304347826098, Timestamp('1980-12-22 00:00:00'): 0.0, Timestamp('1980-12-23 00:00:00'): 0.0, Timestamp('1980-12-24 00:00:00'): 0.0, Timestamp('1980-12-26 00:00:00'): 0.0, Timestamp('1980-12-29 00:00:00'): 0.0, Timestamp('1980-12-30 00:00:00'): -0.02444444444444449, Timestamp('1980-12-31 00:00:00'): -0.05194444444444435, Timestamp('1981-01-02 00:00:00'): -0.04166666666666663, Timestamp('1981-01-05 00:00:00'): -0.0625, Timestamp('1981-01-06 00:00:00'): -0.10416666666666663, Timestamp('1981-01-07 00:00:00'): -0.14222222222222225, Timestamp('1981-01-08 00:00:00'): -0.1597222222222222, Timestamp('1981-01-09 00:00:00'): -0.11444444444444446, Timestamp('1981-01-12 00:00:00'): -0.12166666666666659, Timestamp('1981-01-13 00:00:00'): -0.1527777777777778, Timestamp('1981-01-14 00:00:00'): -0.14916666666666667, Timestamp('1981-01-15 00:00:00'): -0.13194444444444442, Timestamp('1981-01-16 00:00:00'): -0.13888888888888884, Timestamp('1981-01-19 00:00:00'): -0.08694444444444449, Timestamp('1981-01-20 00:00:00'): -0.11444444444444446, Timestamp('1981-01-21 00:00:00'): -0.09722222222222221, Timestamp('1981-01-22 00:00:00'): -0.08694444444444449, Timestamp('1981-01-23 00:00:00'): -0.09027777777777779, Timestamp('1981-01-26 00:00:00'): -0.10416666666666663}, 'Max_Daily_Drawdown': {Timestamp('1980-12-12 00:00:00'): 0.0, Timestamp('1980-12-15 00:00:00'): -0.05217391304347829, Timestamp('1980-12-16 00:00:00'): -0.12173913043478257, Timestamp('1980-12-17 00:00:00'): -0.12173913043478257, Timestamp('1980-12-18 00:00:00'): -0.12173913043478257, Timestamp('1980-12-19 00:00:00'): -0.12173913043478257, Timestamp('1980-12-22 00:00:00'): -0.12173913043478257, Timestamp('1980-12-23 00:00:00'): -0.12173913043478257, Timestamp('1980-12-24 00:00:00'): -0.12173913043478257, Timestamp('1980-12-26 00:00:00'): -0.12173913043478257, Timestamp('1980-12-29 00:00:00'): -0.12173913043478257, Timestamp('1980-12-30 00:00:00'): -0.12173913043478257, Timestamp('1980-12-31 00:00:00'): -0.12173913043478257, Timestamp('1981-01-02 00:00:00'): -0.12173913043478257, Timestamp('1981-01-05 00:00:00'): -0.12173913043478257, Timestamp('1981-01-06 00:00:00'): -0.12173913043478257, Timestamp('1981-01-07 00:00:00'): -0.14222222222222225, Timestamp('1981-01-08 00:00:00'): -0.1597222222222222, Timestamp('1981-01-09 00:00:00'): -0.1597222222222222, Timestamp('1981-01-12 00:00:00'): -0.1597222222222222, Timestamp('1981-01-13 00:00:00'): -0.1597222222222222, Timestamp('1981-01-14 00:00:00'): -0.1597222222222222, Timestamp('1981-01-15 00:00:00'): -0.1597222222222222, Timestamp('1981-01-16 00:00:00'): -0.1597222222222222, Timestamp('1981-01-19 00:00:00'): -0.1597222222222222, Timestamp('1981-01-20 00:00:00'): -0.1597222222222222, Timestamp('1981-01-21 00:00:00'): -0.1597222222222222, Timestamp('1981-01-22 00:00:00'): -0.1597222222222222, Timestamp('1981-01-23 00:00:00'): -0.1597222222222222, Timestamp('1981-01-26 00:00:00'): -0.1597222222222222}}

daily_drawdown列中,在某些实例中,值为0.000。我想在daily_drawdown中提取0.000之间的最小值,最好是新列。

例如,前两个0.000之间的最小值为:-0.121739。稍后在同一列中,将有另一个0.000,我希望它在第二和第三0.000之间选择该最小值,等等。

Definitely easier to show via an example:

{'Close': {Timestamp('1980-12-12 00:00:00'): 28.75, Timestamp('1980-12-15 00:00:00'): 27.25, Timestamp('1980-12-16 00:00:00'): 25.25, Timestamp('1980-12-17 00:00:00'): 25.87, Timestamp('1980-12-18 00:00:00'): 26.63, Timestamp('1980-12-19 00:00:00'): 28.25, Timestamp('1980-12-22 00:00:00'): 29.63, Timestamp('1980-12-23 00:00:00'): 30.88, Timestamp('1980-12-24 00:00:00'): 32.5, Timestamp('1980-12-26 00:00:00'): 35.5, Timestamp('1980-12-29 00:00:00'): 36.0, Timestamp('1980-12-30 00:00:00'): 35.12, Timestamp('1980-12-31 00:00:00'): 34.13, Timestamp('1981-01-02 00:00:00'): 34.5, Timestamp('1981-01-05 00:00:00'): 33.75, Timestamp('1981-01-06 00:00:00'): 32.25, Timestamp('1981-01-07 00:00:00'): 30.88, Timestamp('1981-01-08 00:00:00'): 30.25, Timestamp('1981-01-09 00:00:00'): 31.88, Timestamp('1981-01-12 00:00:00'): 31.62, Timestamp('1981-01-13 00:00:00'): 30.5, Timestamp('1981-01-14 00:00:00'): 30.63, Timestamp('1981-01-15 00:00:00'): 31.25, Timestamp('1981-01-16 00:00:00'): 31.0, Timestamp('1981-01-19 00:00:00'): 32.87, Timestamp('1981-01-20 00:00:00'): 31.88, Timestamp('1981-01-21 00:00:00'): 32.5, Timestamp('1981-01-22 00:00:00'): 32.87, Timestamp('1981-01-23 00:00:00'): 32.75, Timestamp('1981-01-26 00:00:00'): 32.25}, 'Roll_Max': {Timestamp('1980-12-12 00:00:00'): 28.75, Timestamp('1980-12-15 00:00:00'): 28.75, Timestamp('1980-12-16 00:00:00'): 28.75, Timestamp('1980-12-17 00:00:00'): 28.75, Timestamp('1980-12-18 00:00:00'): 28.75, Timestamp('1980-12-19 00:00:00'): 28.75, Timestamp('1980-12-22 00:00:00'): 29.63, Timestamp('1980-12-23 00:00:00'): 30.88, Timestamp('1980-12-24 00:00:00'): 32.5, Timestamp('1980-12-26 00:00:00'): 35.5, Timestamp('1980-12-29 00:00:00'): 36.0, Timestamp('1980-12-30 00:00:00'): 36.0, Timestamp('1980-12-31 00:00:00'): 36.0, Timestamp('1981-01-02 00:00:00'): 36.0, Timestamp('1981-01-05 00:00:00'): 36.0, Timestamp('1981-01-06 00:00:00'): 36.0, Timestamp('1981-01-07 00:00:00'): 36.0, Timestamp('1981-01-08 00:00:00'): 36.0, Timestamp('1981-01-09 00:00:00'): 36.0, Timestamp('1981-01-12 00:00:00'): 36.0, Timestamp('1981-01-13 00:00:00'): 36.0, Timestamp('1981-01-14 00:00:00'): 36.0, Timestamp('1981-01-15 00:00:00'): 36.0, Timestamp('1981-01-16 00:00:00'): 36.0, Timestamp('1981-01-19 00:00:00'): 36.0, Timestamp('1981-01-20 00:00:00'): 36.0, Timestamp('1981-01-21 00:00:00'): 36.0, Timestamp('1981-01-22 00:00:00'): 36.0, Timestamp('1981-01-23 00:00:00'): 36.0, Timestamp('1981-01-26 00:00:00'): 36.0}, 'Daily_Drawdown': {Timestamp('1980-12-12 00:00:00'): 0.0, Timestamp('1980-12-15 00:00:00'): -0.05217391304347829, Timestamp('1980-12-16 00:00:00'): -0.12173913043478257, Timestamp('1980-12-17 00:00:00'): -0.10017391304347822, Timestamp('1980-12-18 00:00:00'): -0.07373913043478264, Timestamp('1980-12-19 00:00:00'): -0.017391304347826098, Timestamp('1980-12-22 00:00:00'): 0.0, Timestamp('1980-12-23 00:00:00'): 0.0, Timestamp('1980-12-24 00:00:00'): 0.0, Timestamp('1980-12-26 00:00:00'): 0.0, Timestamp('1980-12-29 00:00:00'): 0.0, Timestamp('1980-12-30 00:00:00'): -0.02444444444444449, Timestamp('1980-12-31 00:00:00'): -0.05194444444444435, Timestamp('1981-01-02 00:00:00'): -0.04166666666666663, Timestamp('1981-01-05 00:00:00'): -0.0625, Timestamp('1981-01-06 00:00:00'): -0.10416666666666663, Timestamp('1981-01-07 00:00:00'): -0.14222222222222225, Timestamp('1981-01-08 00:00:00'): -0.1597222222222222, Timestamp('1981-01-09 00:00:00'): -0.11444444444444446, Timestamp('1981-01-12 00:00:00'): -0.12166666666666659, Timestamp('1981-01-13 00:00:00'): -0.1527777777777778, Timestamp('1981-01-14 00:00:00'): -0.14916666666666667, Timestamp('1981-01-15 00:00:00'): -0.13194444444444442, Timestamp('1981-01-16 00:00:00'): -0.13888888888888884, Timestamp('1981-01-19 00:00:00'): -0.08694444444444449, Timestamp('1981-01-20 00:00:00'): -0.11444444444444446, Timestamp('1981-01-21 00:00:00'): -0.09722222222222221, Timestamp('1981-01-22 00:00:00'): -0.08694444444444449, Timestamp('1981-01-23 00:00:00'): -0.09027777777777779, Timestamp('1981-01-26 00:00:00'): -0.10416666666666663}, 'Max_Daily_Drawdown': {Timestamp('1980-12-12 00:00:00'): 0.0, Timestamp('1980-12-15 00:00:00'): -0.05217391304347829, Timestamp('1980-12-16 00:00:00'): -0.12173913043478257, Timestamp('1980-12-17 00:00:00'): -0.12173913043478257, Timestamp('1980-12-18 00:00:00'): -0.12173913043478257, Timestamp('1980-12-19 00:00:00'): -0.12173913043478257, Timestamp('1980-12-22 00:00:00'): -0.12173913043478257, Timestamp('1980-12-23 00:00:00'): -0.12173913043478257, Timestamp('1980-12-24 00:00:00'): -0.12173913043478257, Timestamp('1980-12-26 00:00:00'): -0.12173913043478257, Timestamp('1980-12-29 00:00:00'): -0.12173913043478257, Timestamp('1980-12-30 00:00:00'): -0.12173913043478257, Timestamp('1980-12-31 00:00:00'): -0.12173913043478257, Timestamp('1981-01-02 00:00:00'): -0.12173913043478257, Timestamp('1981-01-05 00:00:00'): -0.12173913043478257, Timestamp('1981-01-06 00:00:00'): -0.12173913043478257, Timestamp('1981-01-07 00:00:00'): -0.14222222222222225, Timestamp('1981-01-08 00:00:00'): -0.1597222222222222, Timestamp('1981-01-09 00:00:00'): -0.1597222222222222, Timestamp('1981-01-12 00:00:00'): -0.1597222222222222, Timestamp('1981-01-13 00:00:00'): -0.1597222222222222, Timestamp('1981-01-14 00:00:00'): -0.1597222222222222, Timestamp('1981-01-15 00:00:00'): -0.1597222222222222, Timestamp('1981-01-16 00:00:00'): -0.1597222222222222, Timestamp('1981-01-19 00:00:00'): -0.1597222222222222, Timestamp('1981-01-20 00:00:00'): -0.1597222222222222, Timestamp('1981-01-21 00:00:00'): -0.1597222222222222, Timestamp('1981-01-22 00:00:00'): -0.1597222222222222, Timestamp('1981-01-23 00:00:00'): -0.1597222222222222, Timestamp('1981-01-26 00:00:00'): -0.1597222222222222}}

In the Daily_Drawdown column, there are instances in which the value is 0.000. I want to extract the minimum value of between the 0.000s in Daily_Drawdown, preferably to a new column.

For example, the minimum value between the first two 0.000s is: -0.121739. Later on the same column, there will be another 0.000, and I would like it to pick out that minimum value between the 2nd and 3rd 0.000s, and etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2025-02-20 19:14:09

使用cumsum创建一个石斑鱼,以识别由0隔开的行,然后使用groupbytransform> transform with <代码>最小

g = df['Daily_Drawdown'].eq(0).cumsum()
df['Min_Daily_Drawdown'] = df['Daily_Drawdown'].groupby(g).transform('min')

            Close  Roll_Max  Daily_Drawdown  Max_Daily_Drawdown  Min_Daily_Drawdown
1980-12-12  28.75     28.75        0.000000            0.000000           -0.121739
1980-12-15  27.25     28.75       -0.052174           -0.052174           -0.121739
1980-12-16  25.25     28.75       -0.121739           -0.121739           -0.121739
...
1981-01-21  32.50     36.00       -0.097222           -0.159722           -0.159722
1981-01-22  32.87     36.00       -0.086944           -0.159722           -0.159722
1981-01-23  32.75     36.00       -0.090278           -0.159722           -0.159722
1981-01-26  32.25     36.00       -0.104167           -0.159722           -0.159722

Create a grouper using cumsum to identify the groups of rows separated by 0, then use groupby and transform with min

g = df['Daily_Drawdown'].eq(0).cumsum()
df['Min_Daily_Drawdown'] = df['Daily_Drawdown'].groupby(g).transform('min')

            Close  Roll_Max  Daily_Drawdown  Max_Daily_Drawdown  Min_Daily_Drawdown
1980-12-12  28.75     28.75        0.000000            0.000000           -0.121739
1980-12-15  27.25     28.75       -0.052174           -0.052174           -0.121739
1980-12-16  25.25     28.75       -0.121739           -0.121739           -0.121739
...
1981-01-21  32.50     36.00       -0.097222           -0.159722           -0.159722
1981-01-22  32.87     36.00       -0.086944           -0.159722           -0.159722
1981-01-23  32.75     36.00       -0.090278           -0.159722           -0.159722
1981-01-26  32.25     36.00       -0.104167           -0.159722           -0.159722
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文