pandas - 将存储在数据框中的百分位值应用于数组
问题描述
我有一个百分位数据框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
with np.percentile
。
解决方案
使用 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
推荐阅读
- javascript - 将信息从自动完成添加到新行
- c - 使用 uint8_t 数据的 int 类型矩阵在传递给函数时打印错误
- python - 我想从与给定模式匹配的文件中获取行
- java - 如何在 Java 的 SWT 文本字段中为 JSON 或 XML 语法着色
- ios - 未能建立活动方案
- angular - Angular 在部署后停止工作,mat select 和发送数据出现问题
- android - 动画开始时从视图层次结构中删除的片段
- c# - Rigidbody.MoveRotation:乘以四元数导致恒定旋转
- python - 无论行的时间戳如何,如何合并 2 个数据帧?
- c - 过于频繁地调用 Shell_NotifyIcon 后托盘图标停止出现