python - DataFrame 的派生

标签 python pandas scipy physics derivative

我正在使用两个数据帧,我们将它们命名为时间和电压,它们的形状为 [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电压/d时间),然后绘制(d电压/d时间)与时间的关系图。

我尝试过以下代码:

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)

引发以下错误:

  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 行),因为导数是在两个连续值之间获取的。

关于python - DataFrame 的派生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66669572/

相关文章:

python - 打开同一文件并评估其内容大小,多次运行会产生不同的结果

python - pandas to_datetime() 然后 concat() on DateTime Index

python - 移动平均线或移动平均线

python - 多变量线性回归与 scipy linregress

python - 用python卷积周期图像

python - 如何在 Python 的类中使用静态变量

python - 使用 loc 更新 pandas 数据框中的行无法正常工作

python - 使用 numpy.genfromtxt 读取包含逗号的字符串的 csv 文件

python - Pandas 在过滤时将 int 提升为 float

python - 对起点和终点字符串列表进行排序