Home > Blockchain >  Python: I need to find the average over x amount of rows in a specific column of a large csv file
Python: I need to find the average over x amount of rows in a specific column of a large csv file

Time:10-07

I have a large CSV file with two columns in it as shown below:

enter image description here

I have already filtered the data. I need to calculate the average pressure every x amount of rows.

I've looked for a while on here but was unable to find how to calculate the average every x amount of rows for a specific column. Thanks for any help you can provide.

CodePudding user response:

numpy - average & reshape

n = 3
x = df['Pressure']
  
# calculates the average
avgResult = np.average(givenArray.reshape(-1, n), axis=1)

the result is array, which divide columns into n sets:

eg:

array([3.33333333, 4.66666667])

in:

n=3
x = np.array([1, 4, 5,2,8,4])
  • Related