gpt4 book ai didi

python - 如何对 Pandas 数据框的产品求和?

转载 作者:太空宇宙 更新时间:2023-11-04 04:15:14 25 4
gpt4 key购买 nike

Python 的新手尝试编写一个乘积求和公式,该公式将遍历 pandas 数据框。有小费吗?抱歉,如果这个问题已经被问过。

result = []
i = 0
while i < len(r):
np.prod([1*(1+r)])
result.append(r[i])
i += 1
print(result)

这是我想出的代码,如果有人有更好的方法请告诉我。r 是我的 Pandas 数据框。当我运行这段代码时,我得到的是无限的,它与我实际喜欢的公式不接近

我要实现的公式是

the formula I'm trying to implement

最佳答案

根据你的等式使用 DataFrame.applynumpy.prod

# Example dataframe
df = pd.DataFrame()
df['A'] = [1, 2, 3]
df['B'] = [4, 5, 6]

result = (df + 1).apply(lambda x: np.prod(x))
result

A 24
B 210
dtype: int64

关于python - 如何对 Pandas 数据框的产品求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com