Calculate average of every n rows from a csv file

Saeed :

I have a csv file that has 25000 rows. I want to put the average of every 30 rows in another csv file.

I've given an example with 9 rows as below and the new csv file has 3 rows (3, 1, 2):

|   H    |
 ========
|   1    |---\
|   3    |   |------------->| 3 |
|   5    |---/
|  -1    |---\
|   3    |   |------------->| 1 |
|   1    |---/
|   0    |---\
|   5    |   |------------->| 2 |
|   1    |---/

What I did:

import numpy as np
import pandas as pd

m_path = "ALL0001.CSV"

m_df = pd.read_csv(m_path, usecols=['Col-01']) 
m_arr =  np.array([])
temp = m_df.to_numpy()
step = 30
for i in range(1, 25000, step):
    arr = np.append(m_arr,np.array([np.average(temp[i:i + step])]))

data = np.array(m_arr)[np.newaxis]

m_df = pd.DataFrame({'Column1': data[0, :]})
m_df.to_csv('AVG.csv')

This works well but is there a better solution?

jezrael :

You can use integer division by step for consecutive groups and pass to groupby for aggregate mean:

step = 30
m_df = pd.read_csv(m_path, usecols=['Col-01']) 
df = m_df.groupby(m_df.index // step).mean()

Or:

df = m_df.groupby(np.arange(len(dfm_df// step).mean()

Sample data:

step = 3
df = m_df.groupby(m_df.index // step).mean()
print (df)
   H
0  3
1  1
2  2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python - Calculate average for every column in a csv file

Fast way to take average of every N rows in a .npy array

Python: Calculating Average and Standard deviation for every hour in csv file

Calculate average of each column in a file

How to create a nested dictionary from a csv file with N rows in Python

Calculate monthly value from csv file

Bash: Reading CSV text file and finding average of rows

Calculate average of columns and rows

Add new rows to calculate the sum and average from exiting pandas dataframe

Calculate the average of the rows for each group

Pandas: Calculate the average every 2 rows of a column and put it into the a new column

Extract rows and calculate average

Pick the values from specific lines of a log file and calculate an average

Average every N rows by column

How to calculate Average rating for each movie in R from CSV File?

Calculate the average of the last 12 months for every month?

How to calculate average for every month from start from year in Presto's SQL (Athena)?

How to calculate average of every three consecutive elements

Calculate average from EditTexts

Calculate average from multiple rows

Powershell - Trying to calculate the average of a csv file using a function

How to calculate average from a dropdown excluding the "N/A" values

Fetch values from the selected rows and calculate its average

Calculate average of every 7 instances in a dataframe column

Calculate Average from a file and show records of students whose average is more than 90

Calculate the average sum for every line in shell script

How to calculate the average of rows/selected rows in the matrix

How to calculate average value of different pairs of rows and delete N-1 rows from dataframe?

Pandas Calculate Average Bias By Rows from 2 Columns