【问题标题】:Pandas DataFrame column numerical integrationPandas DataFrame 列数值积分
【发布时间】:2020-12-03 21:17:35
【问题描述】:

目前我有一个如下图所示的DataFrame:

Device   TimeSec  Current  
 1       0.1      0.02
 1       0.25     0.05
 1       0.32     0.07
 1       0.45     0.12
 1       1.32     0.34
 1       2.37     2.24
 2       0.22     0.56
 2       0.34     0.79
 2       1.87     2.76
 2       3.21     3.11
 3       0.16     1.87
 3       1.12     2.33
 3       2.45     3.21
 3       3.45     5.11
 ......

我想对不同的设备进行 Current 与 TimeSec (∫Idt) 的数值积分,并将数据收集到一个新的 DataFrame 中,如下所示:

Device   IntegratedCurrent  
 1         x
 2         y
 3         z

问题是时间间隔不均匀,每个设备的数据数量也不均匀。

非常感谢!

【问题讨论】:

  • 整合的范围应该是多少?
  • ∫Idt 不只是 I (t1 - t0) 吗?即您只需将范围差异乘以当前
  • 我只想根据一个电流和相邻电流的平均值做定积分。
  • 我希望整合为:(t1-t0)*(I1+I0)/2
  • 但是你会对每个平均值都有一个值。例如,Device 1 将有 5 个值,而不仅仅是您在预期数据框输出中编写的一个值

标签: python pandas


【解决方案1】:

使用一些数值积分函数,例如scipy.integrate.trapz

from scipy import integrate

df.groupby(df.Device).apply(lambda g: integrate.trapz(g.Current, x=g.TimeSec))

请注意,此函数使用 trapezoid integration rule,允许指定 x 值。

【讨论】:

猜你喜欢
  • 2016-02-17
  • 2020-03-19
  • 2019-05-20
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多