在矩阵中汇总每个列值

2024-05-16 13:53:30 发布

您现在位置:Python中文网/ 问答频道 /正文

这是数据

 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=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中实现


Tags: inwebforvaluedeviceitemsvalmobile
2条回答

我想我找到了答案

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)

解决方案

我不知道字典通常会有多大。但是您可以使用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],
}

相关问题 更多 >