将存储在数据框中的百分位值应用于数组

Applying percentile values stored in dataframe to an array

我有一个百分位数数据框 df 如下:

            col1   col2     
GDStart                                                                   
2019-09-02  100    11  
2019-09-03   60    16  
2019-09-04   60    67  

我有另一个数组data:

array([1.65 , 1.68 , 1.755, 1.76 , 1.63 ])

我需要使用 df 中的信息执行以下操作以获得百分位数数据帧 dt:

import numpy as np

            col1                       col2     
GDStart                                                                   
2019-09-02  np.percentile(data, 100)   np.percentile(data, 11)
2019-09-03  np.percentile(data, 60)    np.percentile(data, 16)  
2019-09-04  np.percentile(data, 60)    np.percentile(data, 67)  

我不确定如何将 dataframe 映射到 np.percentile

IIUC 你可以使用 apply

df = df.apply(lambda x: np.percentile(data, x))

output:

            col1    col2
GDStart     
2019-09-02  1.76    1.6388
2019-09-03  1.71    1.6428
2019-09-04  1.71    1.7310

使用 listcomp 和 np.transpose

df[:] = np.transpose([np.percentile(data, df[col]) for col in df])

Out[546]:
            col1    col2
GDStart
2019-09-02  1.76  1.6388
2019-09-03  1.71  1.6428
2019-09-04  1.71  1.7310