cuốn sách gpt4 ai đã làm

python - Pandas hoạt động nhưng truy cập các giá trị được tính toán trước đó

In lại Tác giả: Walker 123 更新时间:2023-11-28 17:12:48 28 4
mua khóa gpt4 Nike

假设我有一个像这样的 DataFrame(或 Series):

     Value
0 0.5
1 0.8
2 -0.2
3 None
4 None
5 None

我想创建一个新的Result 列。

每个结果的值由前一个值通过任意函数f确定。

如果之前的值不可用(None 或 NaN),我希望使用之前的结果(当然,并对其应用 f).


使用之前的值很容易,我只需要使用shift。然而,访问以前的结果似乎并不那么简单。

例如,下面的代码计算了结果,但如果需要则无法访问之前的结果。

df['Result'] = df['Value'].shift(1).apply(f)

请假设 f 是任意的,因此使用 cumsum 之类的解决方案是不可能的。


显然,这可以通过迭代来完成,但我想知道是否存在更 Pandas 式的解决方案。

df['Result'] = None
for i in range(1, len(df)):
value = df.iloc[i-1, 'Value']
if math.isnan(value) or value is None:
value = df.iloc[i-1, 'Result']
df.iloc[i, 'Result'] = f(value)

示例输出,给定 f = lambda x: x+1:

差:

   Value Result
0 0.5 NaN
1 0.8 1.5
2 -0.2 1.8
3 NaN 0.8
4 NaN NaN
5 NaN NaN

好:

   Value Result
0 0.5 NaN
1 0.8 1.5
2 -0.2 1.8
3 NaN 0.8
4 NaN 1.8 <-- previous Value not available, used f(previous result)
5 NaN 2.8 <-- same

câu trả lời hay nhất

看起来它对我来说必须是一个循环。我讨厌循环...所以当我循环时,我使用 numba

Numba gives you the power to speed up your applications with high performance functions written directly in Python. With a few annotations, array-oriented and math-heavy Python code can be just-in-time compiled to native machine instructions, similar in performance to C, C++ and Fortran, without having to switch languages or Python interpreters.

https://numba.pydata.org/

from numba import njit


@njit
def f(x):
return x + 1

@njit
def g(a):
r = [np.nan]
for v in a[:-1]:
if np.isnan(v):
r.append(f(r[-1]))
khác:
r.append(f(v))
return r

df.assign(Result=g(df.Value.values))

Value Result
0 0.5 NaN
1 0.8 1.5
2 -0.2 1.8
3 NaN 0.8
4 NaN 1.8
5 NaN 2.8

关于python - Pandas 适用,但访问之前计算的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46421928/

28 4 0
Xem sitemap của VNExpress 之前?

在 MVC3 中,我可以轻松地将 jQuery 脚本标签移动到页面底部“_Layout.vbhtml” 但是,在 ASP.NET MVC3 中,当您使用编辑器模板创建 Controller 时,脚手

  • jquery - 返回到使用 .text() 之前

    悬停时内容被替换,但是当鼠标离开元素时我希望它变回来。我该怎么做? $('.img-wrap').hover(function(){ $(this).find('h4').text('Go

  • C 期望标识符或 ‘(’ 在 ‘{’ 之前

    已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top

  • c - 预计在 elseif 之前

    已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top

  • c - 预期说明符限定符列表位于 'if' 之前

    已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 有关您编写的代码问题的问题必须在问题本身中描述具体问题 - 并包含有效代码以重现该问题。

  • c++ - 预期类型说明符位于 'QwtLog10ScaleEngine' 之前?

    版本:qwt 6.0.1我尝试开发频谱的对数缩放。我使用简单的线条来启用缩放plotspectrum->setAxisScaleEngine(QwtPlot::yLeft, new QwtLog10S

  • c# - 插入触发器 SQL 之前

    我有两个相同的表,I_Subject 和 I_Temp_Subject,我想将 Temp_Subject 表复制到 Subject 表。 I_Temp_Subject 由简单用户使用,I_Subjec

  • javascript - 第一次绘制如何发生在 DOMContentLoaded 之前?

    我的印象是第一次绘制发生在触发 DOMContentLoaded 事件之后。特别是,因为我认为为了让第一次绘制发生,需要渲染树,它依赖于 DOM 构造。另外,我知道 DOM 构造完成时会触发 DOMC

  • Walker 123
    Hồ sơ

    Tôi là một lập trình viên xuất sắc, rất giỏi!

    Nhận phiếu giảm giá taxi Didi miễn phí
    Phiếu giảm giá taxi Didi
    Các bài viết phổ biến trên toàn bộ trang web
    Chứng chỉ ICP Bắc Kinh số 000000
    Hợp tác quảng cáo: 1813099741@qq.com 6ren.com