首页 > 解决方案 > 将存储在数据框中的百分位值应用于数组

问题描述

我有一个百分位数据框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)  

我不确定如何映射dataframewith np.percentile

标签: pandaspython-3.5

解决方案


使用 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

推荐阅读