代码之家  ›  专栏  ›  技术社区  ›  S Meaden

使用pd.cut和pd.vales\u计数,然后将结果作为二维数组

  •  0
  • S Meaden  · 技术社区  · 7 年前

    用例

    1. 然后我用pd.cut将它们按bin分组
    2. 然后我用pd.values\u计数提取值
    3. 我想得到计算的间隔标签和频率计数
    4. 我想将2d数组转换为COM互操作的列表。

    我接近理想的输出,但我是Python新手,所以一些聪明的家伙可以优化我的标签代码。

    这里的问题是最终输出的约束,它需要是一个列表,这样就可以通过COM互操作层将其编组到Excel VBA。

    import inspect
    import numpy as np
    import pandas as pd
    from scipy.stats import skewnorm
    
    pop = skewnorm.rvs(0, size=20)
    bins=[-5,-4,-3,-2,-1,0,1,2,3,4,5]
    bins2 = np.array(bins)
    bins3 = pd.cut(pop,bins2)
    bins4 = [0]*(bins2.size-1)
    
    #print my own labels, doh!
    idx=0
    for binLoop in bins3.categories:
        intervalAsString="(" + str(binLoop.left)+ "," + str(binLoop.right)+"]" 
        print (intervalAsString)
        bins4[idx]=intervalAsString
        idx=idx+1
    
    
    table = pd.value_counts(bins3, sort=False)
    
    joined = np.vstack((bins4,table.tolist()))
    
    print (joined)
    

    目标输出可转换为列表的二维数组

    |  (-5, -4]  |  0  |
    |  (-4, -3]  |  0  |
    |  (-3, -2]  |  0  |
    |  (-2, -1]  |  1  |
    |  (-1, 0]   |  3  |
    |  (0, 1]    |  9  |
    |  (1, 2]    |  4  |
    |  (2, 3]    |  2  |
    |  (3, 4]    |  1  |
    |  (4, 5]    |  0  |
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   grge    7 年前

    pop = skewnorm.rvs(0, size=20)
    bins = range(-5, 5)
    binned = pd.cut(pop, bins)
    
    # create the histogram data
    hist = binned.value_counts()
    
    # hist is a pandas series with a categorical index describing the bins
    # `index.astype(str)` will convert the categories to strings.
    hist.index = hist.index.astype(str)
    
    # `.reset_index()` will turn the index into an ordinary column
    # `.values` gives you the underlying numpy array
    # `tolist()` converts the numpy array to a native python list o' lists.
    print(hist.reset_index().values.tolist())