是否可以使用 sympy Integral 从 pandas Dataframe 生成派生变量?

Is it possible to make derived variables from pandas Dataframe with sympy Integral?

我的问题

您好!是否可以将 pandas DataFrame 与 sympy 包一起使用?

我寻求从 pandas Dataframe 使用 sympy Integral.

制作派生列(变量)的方法

但是我在这些过程中遇到了一些问题。

我该如何解决这个问题?

我的代码

from sympy import *
import pandas as pd
import numpy as np

df = pd.read_csv('https://raw.githubusercontent.com/regenesis90/datasets/master/210521_LSJ_test.csv', encoding = 'cp949')
df


t = Symbol('t')

df['l_star'] = 100 
df['a_i_n'] = 0 
df['b_i_n'] = 0 
df['c_i_n'] = 0

df['L']= df['l'] + df['l_star']
df['Q_rt'] = df['Q_r'] - df['Q_w2']
df['Q_t'] = df['Q_m'] - df['Q_w1']

df['Q'] = df['Q_m'] + df['Q_r']

df['S_r_merging'] = (df['V_m']**2 - df['V_r']**2)/(2 * df['Acc'])

df['t_hat_m'] = 3600/(df['Q_m'] + df['Q_w2'] * (1 - (df['Q_w1']/df['Q_m'])))
df['t_hat_r'] = 3600/(df['Q_r'] + df['Q_w1'] * (1 - (df['Q_w2']/df['Q_r'])))
df['t_hat'] = 3600/df['Q']

df['lambda_m'] = df['K']/(df['t_hat_m'] - df['a'])
df['lambda_r'] = df['K']/(df['t_hat_r'] - df['a'])
df['lambda_t'] = df['K']/(df['t_hat'] - df['a'])


df['func_integ_tf_01'] = t * df['lambda_t']**2 * (t - df['a']) * np.exp(-df['lambda_t']) * np.exp(- df['a']) * exp(t)
df['integ_tf_01'] = Integral(df['func_integ_tf_01'], (t, df['a'], df['T_c'])).doit()

错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-107-6e7da2479649> in <module>
      1 df['func_integ_tf_01'] = t * df['lambda_t']**2 * (t - df['a']) * np.exp(-df['lambda_t']) * np.exp(- df['a']) * exp(t)
----> 2 df['integ_tf_01'] = Integral(df['func_integ_tf_01'], (t, df['a'], df['T_c'])).doit()

C:\dev\Anaconda3\lib\site-packages\sympy\integrals\integrals.py in __new__(cls, function, *symbols, **assumptions)
     85                 useinstead="the as_expr or integrate methods of Poly").warn()
     86 
---> 87         obj = AddWithLimits.__new__(cls, function, *symbols, **assumptions)
     88         return obj
     89 

C:\dev\Anaconda3\lib\site-packages\sympy\concrete\expr_with_limits.py in __new__(cls, function, *symbols, **assumptions)
    492 
    493     def __new__(cls, function, *symbols, **assumptions):
--> 494         pre = _common_new(cls, function, *symbols, **assumptions)
    495         if type(pre) is tuple:
    496             function, limits, orientation = pre

C:\dev\Anaconda3\lib\site-packages\sympy\concrete\expr_with_limits.py in _common_new(cls, function, *symbols, **assumptions)
     46 
     47     if symbols:
---> 48         limits, orientation = _process_limits(*symbols)
     49         for i, li in enumerate(limits):
     50             if len(li) == 4:

C:\dev\Anaconda3\lib\site-packages\sympy\concrete\expr_with_limits.py in _process_limits(*symbols)
    154                         continue
    155 
--> 156         raise ValueError('Invalid limits given: %s' % str(symbols))
    157 
    158     return limits, orientation

**ValueError: Invalid limits given: ((t, 0    1
1    1
2    1
3    1
4    1
Name: a, dtype: int64, 0    3
1    3
2    3
3    3
4    3
Name: T_c, dtype: int64),)**

Integral 不能应用于数组。您必须在数据框的每一行上调用此函数,因此您需要一个循环。在 pandas 中,使用 apply 方法沿 axis 迭代并应用函数。在您的情况下,轴是行,因此传递 axis=columns 以遍历行。

我想你想要这样的东西:

df['integ_tf_01'] = df.apply(lambda sr: Integral(sr['func_integ_tf_01'], (t, sr['a'], sr['T_c'])).doit(), axis="columns")
>>> df['integ_tf_01']
0    -0.118858683067447*E + 0.35657604920234*exp(3)
1    -0.118858683067447*E + 0.35657604920234*exp(3)
2    -0.118858683067447*E + 0.35657604920234*exp(3)
3    -0.118858683067447*E + 0.35657604920234*exp(3)
4    -0.118858683067447*E + 0.35657604920234*exp(3)
Name: integ_tf_01, dtype: object