代码之家  ›  专栏  ›  技术社区  ›  jason m

基于所有列展开窗口的qcut

  •  1
  • jason m  · 技术社区  · 6 年前

    假设我有一个数据帧:

    import numpy as np
    import pandas as pd
    
    df = pd.DataFrame(np.random.normal(0,1,[100,50]))
    

    看起来像:

             0         1         2         3         4         5         6   \
    0 -0.141305  2.158252  1.006520 -1.004185 -0.213160  0.648904 -0.089369   
    1 -1.373167 -1.100959  1.007023  0.699591 -1.667834  1.422182  0.940912   
    2 -0.212014  1.967436  0.401133 -0.996298 -1.696490 -0.857453 -0.686584   
    3 -0.351902  0.413816 -0.494869  0.448740  0.146897 -0.798095 -0.546489   
    4  0.416376 -0.689577 -0.967050 -1.667480  1.223966 -1.382113 -0.812368   
    
             7         8         9     ...           40        41        42  \
    0  0.282299  0.627085  1.111637    ...     1.354044  0.335316 -1.817465   
    1 -0.540302 -1.276811 -0.077210    ...     0.556072  0.642445  0.313477   
    2  0.601571 -0.989826  0.942893    ...     0.803984  0.286897 -0.507413   
    3 -0.277153 -1.068749  1.720561    ...     0.317774  0.744266 -1.671273   
    4  0.391501  0.703358  0.972910    ...    -0.251225 -0.918734  0.226089   
    
             43        44        45        46        47        48        49  
    0 -2.088606 -1.297459 -1.135577 -0.579162 -0.538286  1.223049 -0.577341  
    1  2.307270  0.381122  0.970177  0.011552 -0.704012 -1.759955  0.649379  
    2  0.139226  1.287651  0.335977  0.832819 -0.701925  1.656187  0.218177  
    3  0.621638 -2.893360 -1.349287  2.160106  0.977205 -0.550635 -0.473224  
    4 -0.646419  2.197215 -0.483294 -1.141479  0.706850  2.686787  0.054517 
    

    lbound_ = float(pd.DataFrame(np.ravel(df.iloc[0:10,:].values)).quantile(0.))
    ubound_ = float(pd.DataFrame(np.ravel(df.iloc[0:10,:].values)).quantile(0.1))
    df[(df>=lbound_) & (df<ubound_)]
    

    我想在每一个时间点,根据观察到的任何数据,以扩展的方式对我的数据进行小数/分位数。

    以上仅适用于 0:10 [0,.1) .

    def get_quantile(df,q):
        return np.percentile(df.ravel(),q)
    
    df.expanding().apply(get_quantile,args=(.1,))    
    

    我该如何概括这一点并有效地做到这一点?

    这里有点难懂,请指点。

    谢谢你

    1 回复  |  直到 6 年前
        1
  •  0
  •   jason m    6 年前

    谢谢

    def standardize_block(df_standardize_arg):
        df_standardize_arg = df_standardize_arg.copy()
        ix_ = df_standardize_arg.index
        prior_data = np.array([])
        output = []
        for i in range(0, len(ix_)):
            data = np.array(df_standardize_arg.loc[ix_[i]].values.ravel())
            data = data[~np.isnan(data)]
            prior_data = np.concatenate((prior_data, data), axis=0)
    
            #this date piece may be specific to my use case.
    
            output.append({'date': ix_[i], 
                          'mean': prior_data.mean(), 
                          'std': prior_data.std()}) 
    
        df_output = pd.DataFrame(output)
        del df_standardize_arg #my data is quite large, so I delete it here.
        df_output.index = df_output['date']
        del df_output['date']
        return df_output.copy()