用指定位置的整数数组切片Pandas DataFrame

比尔克(ArtturiBjörk)

我有两个Pandas DataFrame,一个是每一列是一个累积分布(所有条目在之间[0,1]并且单调递增),第二个是与每个累积分布相关联的值。

我需要访问与累积分布(百分位数)中的不同点关联的值。例如,[.1,.9]通过检查应该在第一个DataFrame中的哪个位置插入百分位数,我可能会对百分位数感兴趣,从而在具有相关值的DataFrame中找到这些百分位数的位置。这给了我一个二维的numpy数组,其中每一列都有该列的行的位置。

如何使用此数组访问DataFrame中的值?有没有一种更好的方法可以根据第一个DataFrame中的百分比位置访问其中一个DataFrame中的值?

import pandas pd
import numpy as np

cdfs = pd.DataFrame([[.1,.2],[.4,.3],[.8,.7],[1.0,1.0]])
df1 = pd.DataFrame([[-10.0,-8.0],[1.4,3.3],[5.8,8.7],[11.0,15.0]])
percentiles = [0.15,0.75]
spots = np.apply_along_axis(np.searchsorted,0,cdfs,percentiles)

这不起作用:

df1[spots]

预期产量:

[[1.4 -8.0]
 [5.8 15.0]]

这确实有效,但看起来很麻烦:

output = pd.DataFrame(index=percentiles,columns=df1.columns)
for column in range(spots.shape[1]):
    output.loc[percentiles,column] = df1.loc[spots[:,column],column].values
亨利

尝试这个:

df1.values[spots, [0, 1]]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章