Join lines from a Python column

Asked

Viewed 105 times

1

I’m trying to group my data into one column. I have 43184 lines of data, and I need to group them every 60 data, in case I do a media every 60. I tried to do it and it did not generate error however when I go to visualize the amount of data has more data, so I believe I have not calculated the average, because it would have to have a base of 720 data and generated much more than that. Can someone help me see what I might be putting wrong in the code?

media = []


for i in range (0,len(press),59):


for media1 in press:
    
    
    media1= np.mean(press[i:i+59])

    media.append(media1)

2 answers

0


Your second for is unnecessary. I simulated a DF with the same amount of lines here, and gave a medium list with 720 elements.

df_t = pd.DataFrame(list(range(0, 43184)), columns = ['column'])

media = []
for i in range(0, len(df_t), 60):
    media.append(np.mean(df_t[i:i+59]['column']))

You also need to go to element 60 no for, so that it runs a path between 0 and 59. Otherwise, it goes up to 58, and this should be one of the reasons that generates a list larger than 720;

  • Thanks a lot, I took the other one and it worked. Thanks a lot :D

0

An alternative using pandas groupby

import pandas as pd
import numpy as np

Creating the test data frame

dados = pd.DataFrame({'Dados':np.random.randint(1,100, 43184)})

Calculating the average with Mean directly

dados.groupby(dados.index // 60).mean()

Or using the Aggregate

dados.groupby(dados.index // 60).agg({'Dados':'mean'})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.