【问题标题】:Why is pd.qcut() producing massive boundaries?为什么 pd.qcut() 会产生大量边界?
【发布时间】:2019-08-16 22:06:52
【问题描述】:

我有一个事件数据的数据框,其中一列是该事件发生的时间间隔。我想使用pd.qcut() 来计算每个区间的百分位数,给定其中的事件,并给每个事件各自的百分位数。

def event_quartiler(event_row):
    in_interval = paired_events.loc[events['TimeInterval'] == event_row['TimeInterval']]
    quartiles = pd.qcut(in_interval['DateTime'], 100)
    counter = 1
    for quartile in quartiles.unique():
        if(event_row['DateTime'] in quartile):
            return counter
        counter = counter+1
        if(counter > 100): break
    return -1

events['Quartile'] = events.apply(event_quartiler, axis=1)

我希望这会简单地将四分位数列设置为每个事件的相应百分位数,但代码需要永远运行并通过输出以下内容有效地爆出:

ValueError: ("Bin edges must be unique: array([1.55016605e+18, 1.55016616e+18, 1.55016627e+18, 1.55016632e+18,\n       1.55016632e+18, 1.55016636e+18,
... (I put the ellipsis here because there are 100 data points) 
1.55017534e+18, 1.55017545e+18,\n       1.55017555e+18]).\nYou can drop duplicate edges by setting the 'duplicates' kwarg", 'occurred at index 6539')

6539 处的数据或其间隔内的任何事件都没有什么不同,但我也找不到代码哪里出错了。

【问题讨论】:

  • 我实际上不确定我是否可以...代码一直运行到第 6539 行,然后抛出该问题。那么用我正在谈论的类型制作一个数据框并将其发布在这里是否合理?抱歉,我是新手,不太了解这里的标准。

标签: python pandas dataframe valueerror


【解决方案1】:

我发现了问题:qcut 尝试将所有数据点本身拟合成四分位数,而 cut 取最小值和最大值并切入 n 个 bin。因为在此示例中,我尝试制作的四分位数比实际数据点多,所以 qcut 失败了。

仅使用 cut into 100 bins 就解决了我的问题,并且我能够制作百分位数。

【讨论】:

    猜你喜欢
    • 2013-06-06
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 2020-08-05
    • 2012-08-08
    相关资源
    最近更新 更多