0

I have the following pandas dataframe df with 2 columns, which looks like:

0  0
1. 22
2. 34
3. 21
4. 21
5. 92

I would like to integrate the area under this curve if we were to plot the first columns as the x-axis and the second column as the y-axis. I have tried doing this using the integrated module from scipy (from scipy import integrate), and applied as follows as I have seen in examples online:

print(df.integrate)

However, it seems the integrate function does not work. I'm receiving the error:

Dataframe object has no attribute integrate

How would I go about this?

Thank you

3 Answers 3

1

You want numerical integration given a fixed sample of data. The Scipy package lists a handful of methods to do this: https://docs.scipy.org/doc/scipy/reference/integrate.html#integrating-functions-given-fixed-samples

For your data, the trapezoidal is probably the most straight forward. You provide the y and x values to the function. You did not post the column names of your data frame, so I am using the 0-index for x and the 1-index for y values

from scipy.integrate import trapz

trapz(df.iloc[:, 1], df.iloc[:, 0])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer! I have applied your code as listed above (my x-axis is 'time' and y-axis is 'fuel', they are both strings I believe). When running the following line: print (trapz(df.iloc[:,'fuel [m3'], df.iloc[:, 'time [s]'])) I get the following ValueError however: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types
0

Since integrate is a scipy method not a pandas method, you need to invoke it as follows:

from scipy.integrate import trapz, simps
print(trapz(*args))

https://docs.scipy.org/doc/scipy/reference/tutorial/integrate.html

2 Comments

Thanks for you answer and the link. My question now is how do I get the first column as x-axis and second as y-axis in for example a list? I need to do this right to perform the following: integral = simps(y,x) as per the example in your link?
df['x'].to_list() OR df.x.values, the first one gives you a list the second is pandas native method.
0

Try this

import pandas as pd
import numpy as np

def integrate(x, y):
    area = np.trapz(y=y, x=x)
    return area

df = pd.DataFrame({'x':[0, 1, 2, 3, 4, 4, 5],'y':[0, 1, 3, 3, 5, 6, 7]})
x = df.x.values
y = df.y.values
print(integrate(x, y))

2 Comments

Thanks for your answer! However, with this method I need to fill in the x and y axis by hand, but what if these values are a very big column? How would I get the x and y axes separately from the dataframe?
The dataframe mentioned is just a sample pandas dataframe. You can get the column values with df['col_name'].values (replace column name with your x values or y values columns)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.