Python - Pandas Dataframe get avarage of n rows

Viewed 72

Just to make that clear at the start. I'm talking about rows and not about columns:

https://i.stack.imgur.com/jikOF.png

I am working with a pandas Dataframe that saves multiple decibel Values. My job is it to get the average of n rows. I get the values from an .wav file where I run an stft function to save the data saved in the .wav file into an pd.Dataframe. Because I work with large files I have to aggregate the Dataframe in order to save DiskSpace.

I already have an algorithm that calculates n, the aggregation, so I only need to run the command to do so.

for example: When the aggregation n is 4 the Dataframe should be converted from:

this:

2, 4, 6, 8, 10, 12, 14, 16  ...

3, 6, 9, 12, 15, 18, 21, 24 ...
...

into this:

{2, 4, 6, 8}, {10, 12, 14, 16}  ... ,{3, 6, 9, 12}, {15, 18, 21, 24} ...

to this:

{5}, {13} ... {7.5}, {19.5} ...

and in the end to a new Dataframe:

5, 13, ...
7.5, 19.5, ...
...

This is the Dataframe I'm working with:

         0          1          2    ...        510        511        512

 0  -50.148830 -50.731472 -58.459068  ... -80.000000 -80.000000 -80.000000

 1  -49.234821 -49.215736 -57.735603  ... -80.000000 -80.000000 -80.000000

 2  -53.309082 -53.790737 -61.091209  ... -80.000000 -80.000000 -80.000000

 3  -50.511078 -51.120682 -59.740089  ... -80.000000 -80.000000 -80.000000

 4  -52.077423 -51.985920 -59.107773  ... -80.000000 -80.000000 -80.000000

 5  -47.280487 -47.177921 -56.507130  ... -80.000000 -80.000000 -80.000000

so when we take a look how the first line should change when n is three:

 0  -50.148830 -50.731472 -58.459068  ... -80.000000 -80.000000 -80.000000

They get "grouped":

 array = [{-50.148830, -50.731472, -58.459068}, ..., {-80.000000, -80.000000, -80.000000}, ...];

Then the avarage/mean()? the array

array = [{-53.1131}, ..., {-80.000000}, ...];

But instead of doing this to only one line do it to all of them and convert them back into an pd.Dataframe.

        0     ...    512

 0  -53.1131  ... -80.000000

 1  -52.0620  ... -80.000000

 2  -56.0636  ... -80.000000

 3  -53.7906  ... -80.000000

 4  -54.3904  ... -80.000000

 5  -50.3219  ... -80.000000

I tried the groupby function but I always get an Value Error:

n = 3
pd.Dataframe = data

grouped = data.groupby(np.arange(len(data)) // n, axis=1).mean()

output:

ValueError: Grouper and axis must be same length

I couldn't find a solution on the internet so any help is appreciated :D

1 Answers

'The grouper and axis should be of same length': In your code, the grouper here is len(data) (number of rows) and axis is 1 which are actually different. So it should be either

data.groupby(np.arange(len(data)) // n, axis=0).mean()

or

data.groupby(np.arange(len(data.columns)) // n, axis=1).mean()

In your case, it seems that you want to groupby by columns, so the latter version should work

Related