gpt4 book ai didi

python - python pandas 数据框中的定数值积分

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

我有一个列数可变的 pandas 数据框。我想对数据帧的每一列进行数值积分,以便我可以计算从第 0 行到第“n”行的定积分。我有一个适用于一维数组的函数,但是有没有更好的方法可以在 pandas 数据框中执行此操作,以便我不必迭代列和单元格?我正在考虑使用 applymap 的某种方法,但我不知道如何使其发挥作用。

这是作用于一维数组的函数:

    def findB(x,y):

y_int = np.zeros(y.size)
y_int_min = np.zeros(y.size)
y_int_max = np.zeros(y.size)
end = y.size-1

y_int[0]=(y[1]+y[0])/2*(x[1]-x[0])

for i in range(1,end,1):
j=i+1
y_int[i] = (y[j]+y[i])/2*(x[j]-x[i]) + y_int[i-1]

return y_int

我想将其替换为同时计算数据帧的多个列的东西,如下所示:

    B_df = y_df.applymap(integrator)  

编辑:

起始数据帧dB_df:

        Sample1 1 dB    Sample1 2 dB    Sample1 3 dB    Sample1 4 dB Sample1 5 dB   Sample1 6 dB
0 2.472389 6.524537 0.306852 -6.209527 -6.531123 -4.901795
1 6.982619 -0.534953 -7.537024 8.301643 7.744730 7.962163
2 -8.038405 -8.888681 6.856490 -0.052084 0.018511 -4.117407
3 0.040788 5.622489 3.522841 -8.170495 -7.707704 -6.313693
4 8.512173 1.896649 -8.831261 6.889746 6.960343 8.236696
5 -6.234313 -9.908385 4.934738 1.595130 3.116842 -2.078000
6 -1.998620 3.818398 5.444592 -7.503763 -8.727408 -8.117782
7 7.884663 3.818398 -8.046873 6.223019 4.646397 6.667921
8 -5.332267 -9.163214 1.993285 2.144201 4.646397 0.000627
9 -2.783008 2.288842 5.836786 -8.013618 -7.825365 -8.470759

结束数据帧B_df:

        Sample1 1 B Sample1 2 B Sample1 3 B Sample1 4 B Sample1 5 B Sample1 6 B
0 0.000038 0.000024 -0.000029 0.000008 0.000005 0.000012
1 0.000034 -0.000014 -0.000032 0.000041 0.000036 0.000028
2 0.000002 -0.000027 0.000010 0.000008 0.000005 -0.000014
3 0.000036 0.000003 -0.000011 0.000003 0.000002 -0.000006
4 0.000045 -0.000029 -0.000027 0.000037 0.000042 0.000018
5 0.000012 -0.000053 0.000015 0.000014 0.000020 -0.000023
6 0.000036 -0.000023 0.000004 0.000009 0.000004 -0.000028
7 0.000046 -0.000044 -0.000020 0.000042 0.000041 -0.000002
8 0.000013 -0.000071 0.000011 0.000019 0.000028 -0.000036
9 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

在上面的例子中,

    (x[j]-x[i]) = 0.000008

最佳答案

首先,您可以使用矢量化运算获得类似的结果。积分的每个元素只是当前和下一个 y 值按 x 中相应差值缩放的平均值。最终的积分就是这些元素的累加和。您可以通过执行类似的操作来获得相同的结果

def findB(x, y):
"""
x : pandas.Series
y : pandas.DataFrame
"""
mean_y = (y[:-1] + y.shift(-1)[:-1]) / 2
delta_x = x.shift(-1)[:-1] - x[:-1]
scaled_int = mean_y.multiply(delta_x)
cumulative_int = scaled_int.cumsum(axis='index')
return cumulative_int.shift(1).fillna(0)

这里DataFrame.shiftSeries.shift用于将“下一个”元素的索引与当前元素进行匹配。您必须使用DataFrame.multiply而不是 * 运算符,以确保使用正确的轴('index''column')。最后,DataFrame.cumsum提供最终的集成步骤。 DataFrame.fillna确保您的第一行是零,就像在原始解决方案中所做的那样。使用所有原生 pandas 函数的优点是,您可以传入具有任意数量列的数据帧,并让它同时对所有列进行操作。

关于python - python pandas 数据框中的定数值积分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900677/

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