微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数据帧的派生

如何解决数据帧的派生

我正在处理两个数据帧,让我们将它们命名为时间和电压,它们的形状是 [2201 行 x 8 列]。

数据帧的第一行和最后一行是(分别是时间和电压):

               A = 2.90 V           A = 3.00 V           A = 3.10 V           A = 3.20 V           A = 3.30 V           A = 3.40 V           A = 3.50 V           A = 4.90 V
0            0.000000e+00         0.000000e+00         0.000000e+00         0.000000e+00         0.000000e+00         0.000000e+00         0.000000e+00         0.000000e+00
1            2.000000e-09         2.000000e-09         2.000000e-09         2.000000e-09         2.000000e-09         2.000000e-09         2.000000e-09         2.000000e-09
2            4.000000e-09         4.000000e-09         4.000000e-09         4.000000e-09         4.000000e-09         4.000000e-09         4.000000e-09         4.000000e-09
3            6.000000e-09         6.000000e-09         6.000000e-09         6.000000e-09         6.000000e-09         6.000000e-09         6.000000e-09         6.000000e-09
4            8.000000e-09         8.000000e-09         8.000000e-09         8.000000e-09         8.000000e-09         8.000000e-09         8.000000e-09         8.000000e-09
...                   ...                  ...                  ...                  ...                  ...                  ...                  ...                  ...
2196         4.392000e-06         4.392000e-06         4.392000e-06         4.392000e-06         4.392000e-06         4.392000e-06         4.392000e-06         4.392000e-06
2197         4.394000e-06         4.394000e-06         4.394000e-06         4.394000e-06         4.394000e-06         4.394000e-06         4.394000e-06         4.394000e-06
2198         4.396000e-06         4.396000e-06         4.396000e-06         4.396000e-06         4.396000e-06         4.396000e-06         4.396000e-06         4.396000e-06
2199         4.398000e-06         4.398000e-06         4.398000e-06         4.398000e-06         4.398000e-06         4.398000e-06         4.398000e-06         4.398000e-06
2200         4.400000e-06         4.400000e-06         4.400000e-06         4.400000e-06         4.400000e-06         4.400000e-06         4.400000e-06         4.400000e-06

[2201 rows x 8 columns]
               A = 2.90 V           A = 3.00 V           A = 3.10 V           A = 3.20 V           A = 3.30 V           A = 3.40 V           A = 3.50 V           A = 4.90 V
0                0.003537             0.007219             0.012674             0.017294             0.022206             0.027240             0.032120             0.106918
1                0.003532             0.007214             0.012666             0.017288             0.022212             0.027280             0.032082             0.106855
2                0.003537             0.007217             0.012677             0.017342             0.022264             0.027290             0.032008             0.106764
3                0.003535             0.007221             0.012671             0.017352             0.022236             0.027307             0.032054             0.106809
4                0.003539             0.007224             0.012675             0.017336             0.022244             0.027320             0.032064             0.106836
...                   ...                  ...                  ...                  ...                  ...                  ...                  ...                  ...
2196             0.000399             0.000490             0.000579             0.000164             0.000176             0.000230             0.000238             0.000336
2197             0.000406             0.000495             0.000578             0.000146             0.000216             0.000237             0.000252             0.000227
2198             0.000405             0.000495             0.000577             0.000188             0.000192             0.000273             0.000230             0.000264
2199             0.000402             0.000494             0.000573             0.000138             0.000216             0.000193             0.000240             0.000200
2200             0.000408             0.000492             0.000572             0.000170             0.000210             0.000253             0.000224             0.000373

我可以轻松绘制每列的曲线(电压与时间)。 我想根据时间数据帧获得电压数据帧的导数,或者它在数学中的相同之处(d电压/dtime),然后绘制(d电压/dtime)与时间的关系图。

我尝试了以下代码

def Derivative():
    dV_dt = []
    for c in range(len(V.columns)):
        dn = derivative(V.iloc[:,c],time.iloc[:,dx=time.iloc[:,c])
        dV_dt.append(dn)
    dV_dt = pd.DataFrame(dV_dt)
    print(dV_dt)

引发以下错误

  File "C:\ProgramData\Anaconda3\Lib\site-packages\scipy\misc\common.py",line 119,in derivative
    val += weights[k]*func(x0+(k-ho)*dx,*args)
TypeError: 'Series' object is not callable

有没有人解决这个问题?提前致谢。

解决方法

您可以尝试以下操作:

import pandas as pd
import numpy as np
V = pd.DataFrame(np.random.rand(2201,8)*10)
time = pd.DataFrame(np.random.rand(2201,8))
dV_dt = pd.DataFrame([np.diff(V[i])/np.diff(time[i]) for i in V.columns])
print (V,time,dV_dt)

请注意,生成的 dV_dt 比原始的两个数据帧少一行(仅 2200 行)。因为导数是在两个结果值之间取的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。