Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame column numerical integration

Tags:

python

pandas

Currently I have a DataFrame as shown below:

Device   TimeSec  Current  
 1       0.1      0.02
 1       0.25     0.05
 1       0.32     0.07
 1       0.45     0.12
 1       1.32     0.34
 1       2.37     2.24
 2       0.22     0.56
 2       0.34     0.79
 2       1.87     2.76
 2       3.21     3.11
 3       0.16     1.87
 3       1.12     2.33
 3       2.45     3.21
 3       3.45     5.11
 ......

I would like to do the numerical integration of Current with TimeSec (∫Idt) for different Devices and collect the data into a new DataFrame as below:

Device   IntegratedCurrent  
 1         x
 2         y
 3         z

The problem is that the time interval is not even and the number of data for each device is not even as well.

Many thanks!

like image 489
FunkyMore Avatar asked May 08 '18 18:05

FunkyMore


People also ask

How do I convert data to numeric in pandas?

The best way to convert one or more columns of a DataFrame to numeric values is to use pandas.to_numeric() . This function will try to change non-numeric objects (such as strings) into integers or floating-point numbers as appropriate.

Can you use Numba with pandas?

Numba can be used in 2 ways with pandas: Specify the engine="numba" keyword in select pandas methods. Define your own Python function decorated with @jit and pass the underlying NumPy array of Series or DataFrame (using to_numpy() ) into the function.

Do pandas series have index?

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.

How does pandas arrange data in descending order?

In order to sort the data frame in pandas, function sort_values() is used. Pandas sort_values() can sort the data frame in Ascending or Descending order.


1 Answers

Use some numerical integration function, e.g., scipy.integrate.trapz:

from scipy import integrate

df.groupby(df.Device).apply(lambda g: integrate.trapz(g.Current, x=g.TimeSec))

Note that this function, using the trapezoid integration rule, allows to specify the x values.

like image 120
Ami Tavory Avatar answered Oct 18 '22 06:10

Ami Tavory