【问题标题】:Return the bin of a certain value after splitting into Pandas bins after CUT or value_counts()在 CUT 或 value_counts() 后拆分为 Pandas bins 后返回某个值的 bin
【发布时间】:2021-06-15 13:26:55
【问题描述】:

假设我使用df.value_counts(bins=10) 分割我的框架 这就是它的样子

Values_mean
(53.649, 90.21]                  5127
(35.369, 53.649]                 4285
(90.21, 108.49]                  3559
(108.49, 126.77]                 2579
(866.77, 935.05]                 1526
(199.891, 218.171]               1304
(218.171, 251.451]                506
(-1.46699, 17.089]                478
(251.451, 284.732]                 30
(284.732, 343.012]                  7
Name: Values_mean, dtype: int64

现在我想找出值在哪里:newVal=38.54 是否适合以及它在哪个百分比的数据中。

示例:这些值的 CUMSUM = 19401 newVal=38.54 将属于 Bin

(35.369, 53.649]                 4285

所以它表示 4285/19401,它属于 0.2208 的数据。

提前谢谢你

【问题讨论】:

    标签: python pandas dataframe numpy data-fitting


    【解决方案1】:

    您想要对索引进行排序,然后使用right 属性来获取间隔的右侧。然后使用searchsorted 查找适合搜索值的位置。使用该信息计算计数值。

    def ptile(x, s):
        total = s.sum()
        s = s.sort_index()
        i = s.index.right.searchsorted(x)
        return s.iloc[i] / total
    
    ptile(38.54, s)
    
    0.2208649038709345
    

    【讨论】:

    • 工作就像一个魅力,谢谢;)
    猜你喜欢
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2013-09-17
    相关资源
    最近更新 更多