bbartling
bbartling

Reputation: 3494

Pandas create additional data frames with for loop

For SO purposes, this is some made up times series data:

import pandas as pd 
import numpy as np 
from numpy.random import randint 

np.random.seed(10)  # added for reproductibility                                                                                                                                                                 

rng = pd.date_range('10/9/2018 00:00', periods=1000, freq='1H') 
df = pd.DataFrame({'Random_Number':randint(1, 100, 1000)}, index=rng) 

Question, how do I create a function that can return re-sampled daily 97.5 & 2.5 percentiles values for each in day in pandas data frame? I know this code below isn't even close it will just return the upper & lower percentiles of the entire dataset. Ultimately Im trying to break this down per day and the dataframe returned the index would be the time stamp (date) of the day re sampled..

def createDfs(data):
    for day in df:
        dfDay = pd.DataFrame()

        hi = df.quantile(0.975)[0]
        low = df.quantile(0.025)[0]

        data = {'upper_97.5%': [hi],
                'lower_2.5%' : [low]}     

        dfUpperLower = pd.DataFrame(data)
        #dfUpperLower.set_index('Date')

    return dfUpperLower

Any tips greatly appreciated..

Upvotes: 0

Views: 30

Answers (1)

Randy
Randy

Reputation: 14849

I think you just want to use .resample with .quantile:

In [10]: df.resample('1D').quantile([0.025, 0.975]).unstack()
Out[10]:
           Random_Number
                   0.025   0.975
2018-10-09         5.600  91.700
2018-10-10        12.575  94.425
2018-10-11         5.575  92.400
2018-10-12         9.875  97.425
2018-10-13         2.725  87.550
2018-10-14        10.200  96.425
2018-10-15        10.725  96.425
...

Upvotes: 2

Related Questions