Home > database >  How to analyze data from one df to every n data?
How to analyze data from one df to every n data?

Time:03-06

I'm currently working with a pandas df where I need to perform an analysis for each 96 datas so far I am doing the process manually usind .loc function :

df_partial1 = df.loc[0:95]
df_partial2  = df.loc[96:191]

And so on . For each df_partial I'm applying a function called bill. I'd like to automate this process in order to make it more efficient. Something like this:

a = [] # empty list 
for df_partial (each 96 points) in the df:
        a = bill(df_partial)
        a = a.append(a) # A list with each result of the function for each df_partial

Can anyone help me with this?

CodePudding user response:

You can try to have a look at window functions with pandas. I believe the first example would help you: https://pandas.pydata.org/pandas-docs/stable/user_guide/window.html

It also have an apply method which allows you to directly apply the function, instead of iterating through the windows and call your function.

CodePudding user response:

Use groupby and a custom group:

import numpy as np

group = np.arange(len(df))//96
# group 0 is 0:95, group 1 is 96:191, etc.

a = [bill(d) for _,d in df.groupby(group)]
  • Related