【问题标题】:Rolling up each columnar value in a matrix汇总矩阵中的每个列值
【发布时间】:2020-09-26 12:29:21
【问题描述】:

这是数据。

 day_value = {
        'android':[1,0,0,0,0,0,1],
         'iphone':[0,1,0,1,0,0,0],
            'web':[0,1,1,0,1,0,0],
     }


device_rollup = {
    'overall':['iphone','android','web'],
     'mobile':['iphone','android'],
 }

rollup_l7 = {
    'overall': 6,
     'mobile': 4,
 }

如果每一列的值都相同,我们必须汇总为 1 而不是将其相加。总数应为 6 ((1,0,0)+(0,1,1)+(0,0,1)+(0,1,0)+ (0,0,1)+ (1 ,0,0)-> 1+1+1+1+1+1 =6)

现在我可以像这样将所有值相加。

overall_val = sum(sum(v) for k,v in day_value.items() if k in device_rollup['overall'] )
mobile_val = sum(sum(v) for k,v in day_value.items() if k in device_rollup['mobile'] )

rollup_l7= {'overall':overall_val, 'mobile':mobile_val}
print(rollups_l7)

但是,我不想累加,而是想累加每个列值。

我们需要将列表转换为二进制然后再转换回整数吗?

我不太确定如何在不使用 numpy 的情况下在常规 python 中实现

【问题讨论】:

  • 请用一个例子解释你期望的输出。
  • @CypherX - 使用示例更新了详细信息
  • 据我所知,如果任何行有 1,则需要加 1。这样是否正确?
  • 一种。它是灯中每个值的二进制或

标签: python pandas rollup


【解决方案1】:

我想我找到了答案。

overall = sum([x | y | z  for x,y,z in zip(day_value['android'], day_value['iphone'],day_value['web'])])
mobile = sum([x | y for x,y in zip(day_value['android'], day_value['iphone'])])

rollup_l7 = {'overall':overall, 'mobile':mobile}
print(rollup_l7)

【讨论】:

    【解决方案2】:

    解决方案

    我通常不知道字典 day_value 会有多大。但是您仍然可以使用pandas 作为替代方案。以下代码块将按照您的说明处理所有数据。

    # pip install pandas
    import pandas as pd
    
    ## define label categories for modularity
    categories = {
        'overall': ['android', 'iphone', 'web'], 
        'mobile': ['android', 'iphone'], 
        'rollup': ['overall', 'mobile']  
    }
    
    ## create a dataframe with input data
    ## and process for 'overall' and 'mobile'
    df = pd.DataFrame(day_value)
    df['overall'] = df[categories['overall']].sum(axis=1) > 0
    df['mobile'] = df[categories['mobile']].sum(axis=1) > 0
    
    ## evaluate totals of all columns in df
    total = df.sum(axis=0).astype(int)
    
    ## update device_rollup
    device_rollup = {'overall': [], 'mobile': []}
    for k, v in total[categories['overall']].to_dict().items(): 
        print(k, v)
        if v > 0:
            device_rollup['overall'].append(k)
            if k in categories['mobile']:
                device_rollup['mobile'].append(k) 
    
    ## update rollup_l7
    rollup_l7 = total[categories['rollup']].to_dict()
    

    输出

    ## df: dataframe
    # print(df)
       android  iphone  web  overall  mobile
    0        1       0    0     True    True
    1        0       1    1     True    True
    2        0       0    1     True   False
    3        0       1    0     True    True
    4        0       0    1     True   False
    5        0       0    0    False   False
    6        1       0    0     True    True
    
    ## total
    # print(total.to_dict())
    {'android': 2, 'iphone': 2, 'mobile': 4, 'overall': 6, 'web': 3}
    
    ## device_rollup
    # print(device_rollup)
    {
        'mobile': ['android', 'iphone'], 
        'overall': ['android', 'iphone', 'web']
    }
    
    ## rollup_l7
    # print(rollup_l7)
    {'mobile': 4, 'overall': 6}
    

    数据

    day_value = {
        'android': [1,0,0,0,0,0,1],
        'iphone':  [0,1,0,1,0,0,0],
        'web':     [0,1,1,0,1,0,0],
    }
    

    【讨论】:

    • @paddu 请试试这个,如果您有任何问题,请告诉我。