PANDAS-使用第二个DataFrame作为该函数的输入,在列上应用功能?

发布于 2025-02-04 04:25:59 字数 638 浏览 1 评论 0原文

寻找最快,最pandas以以下方式进行以下方式:

support=

Values Confidence R/S
10      3          S
20      6          S
40      10         S
35      12         S
df = 

name    strike
xyz      12
dfg      6
ghf      40

目标:从支持df的最接近0行。

挖掘输出:

df = 

name    strike   support
xyz      12      [10, 3, S, 2]
dfg      6       [0, 0, S, 0]   # as there is no > 0 value when subtracting strike from support
ghf      40      [35, 12, S, 5]

奖金:将列扩展到相关的列中。

我可以通过进行罢工来做到这一点,想知道是否有更好/更快的方法来实现我在想做的事情。

Look for the fastest and most pandas centric way of doing the following:

support=

Values Confidence R/S
10      3          S
20      6          S
40      10         S
35      12         S
df = 

name    strike
xyz      12
dfg      6
ghf      40

Aim: Get the closest greater than 0 row from support to df.

Excpected output:

df = 

name    strike   support
xyz      12      [10, 3, S, 2]
dfg      6       [0, 0, S, 0]   # as there is no > 0 value when subtracting strike from support
ghf      40      [35, 12, S, 5]

Bonus: expand the columns into the relevant columns.

I can do this by looping through the strikes, wondering if there is a better/faster way to achieve what I am thinking of doing.

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

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

发布评论

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

评论(1

清音悠歌 2025-02-11 04:25:59

使用

pd.merge_asof(df.sort_values('strike'),       # must be sorted by key
              support.sort_values('Values'),  # must be sorted by key
              left_on='strike', 
              right_on='Values', 
              direction='backward',           # default, so `Values <= strike`
              allow_exact_matches=False       # so that `Values != strike`
             )

输出:

  name  strike  Values  Confidence  R/S
0  dfg       6     NaN         NaN  NaN
1  xyz      12    10.0         3.0    S
2  ghf      40    35.0        12.0    S

Use merge_asof

pd.merge_asof(df.sort_values('strike'),       # must be sorted by key
              support.sort_values('Values'),  # must be sorted by key
              left_on='strike', 
              right_on='Values', 
              direction='backward',           # default, so `Values <= strike`
              allow_exact_matches=False       # so that `Values != strike`
             )

Output:

  name  strike  Values  Confidence  R/S
0  dfg       6     NaN         NaN  NaN
1  xyz      12    10.0         3.0    S
2  ghf      40    35.0        12.0    S
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文