Search code examples
pythonpandasdataframeintegration

How to integrate over pandas data frame with non constant Jacobin


I have a pandas data frame which looks like this:

E     p     S
.1   .1     4
.1.  .2     5
.    .      .
.    .      .
.3   .1     2
.3.  .2     4
.    .      .
.    .      .
15.  .1     5
15.  .2     9
.    .      .
.    .      .

As you see for each value of E there are multiple values of p and S. I'm trying to numerically perform the integral $n(E) = \Int dp p^2 S$ for each value of E. Unfortunately due to the p^2 factor in the integral I don't think I can use groupby(['E']).sum() because that will only perform the sum over S without the p^2 factor. Any way to do this integral in a nice pythonic way would be appreciated.


Solution

  • Using numpy's trapezoidal formula you can do:

    df.groupby('E').apply(lambda x: np.trapz(x['S'].mul(x['p']**2), x['p']))