Pandas 将存储在dataframe中的百分位值应用于数组
我有一个百分位数据框Pandas 将存储在dataframe中的百分位值应用于数组,pandas,python-3.5,Pandas,Python 3.5,我有一个百分位数据框df,如下所示: col1 col2 GDStart 2019-09-02 100 11 2019-09-03 60 16 2019-09-04 60 67 我有另一个数组数据: array([1.65 , 1.68 , 1.755, 1.76 , 1.63 ])
df
,如下所示:
col1 col2
GDStart
2019-09-02 100 11
2019-09-03 60 16
2019-09-04 60 67
我有另一个数组数据
:
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)
我不知道如何将
数据帧
映射到np.百分位数
IIUC,您可以使用应用
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
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
使用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
是否要使用所有5个数组数据来计算每列的百分比?是的,基本上使用
df
中的数据来计算数据的百分比
是否要使用所有5个数组数据来计算每列的百分比?是,基本上使用df
中的数据来计算数据的百分比
是否有必要使用['col1','col2']
您不能在datetime列上应用百分比。datetime
列是索引。不过,您是对的,不需要选择列,我会更新答案是否有必要使用['col1','col2']
您不能在datetime列上应用百分位数。datetime
列是索引,但是哦,您是对的,不需要选择列,我会更新答案