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

Calculate average of every n rows in pandas and assign new labels to rows

Calculate the average date every x rows

Average every N rows by column

Calculate average from multiple rows

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

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

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

Calculate average of columns and rows

Extract rows and calculate average

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

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

Fetch values from the selected rows and calculate its average

Pandas Calculate Average Bias By Rows from 2 Columns

How to calculate moving average from previous rows in data.table?

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

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

How to print random n rows from a csv file?

Calculate the average of the rows for each group

How to calculate average of adjacent rows?

Bash: Reading CSV text file and finding average of rows

Calculate Slope for every n rows with respect to a conditioned row per group

R: Calculate variables of every n rows while keeping the date/timestamp

Calculate average from EditTexts

Python + Pandas split CSV file by rows with headers/columns in every file

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

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

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