简体   繁体   English

DataFrame 的衍生物

[英]Derivative of a DataFrame

I am working with two dataframes, let's name them time and voltage, and their shape is [2201 rows x 8 columns].我正在使用两个数据框,我们将它们命名为时间和电压,它们的形状是 [2201 行 x 8 列]。

The first and last rows of the dataframes are (time and voltage respectively):数据帧的第一行和最后一行是(分别是时间和电压):

               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

I can eaily plot the curves for each column (voltage vs time).我可以很容易地 plot 每列的曲线(电压与时间)。 I would like to get the derivative of the voltage dataframe respect the time dataframe, or what it is the same in mathemetics (dvoltage/dtime) and then plot (dvoltage/dtime) vs time.我想得到电压 dataframe 的导数,尊重时间 dataframe,或者它在数学上是一样的(dvoltage/dtime)然后是 Z32FA6E1B78A9D4028953E60564 与时间。

I have tried the following code:我尝试了以下代码:

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

raising the following error:引发以下错误:

  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

Does anyone how to solve this?有人如何解决这个问题? Thanks in advance.提前致谢。

You can try the following:您可以尝试以下方法:

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)

please note the the resultant dV_dt is one row less (2200 rows only) than the original two dataframes.请注意,生成的 dV_dt 比原始的两个数据帧少一行(仅 2200 行)。 as the derivative is taken between two consequential values.因为导数是在两个结果值之间取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM