汇总矩阵中的每个列值
Rolling up each columnar value in a matrix
这是数据。
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
我想我找到了答案。
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)
解决方案
我通常不知道字典 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],
}
这是数据。
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我想我找到了答案。
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)
解决方案
我通常不知道字典 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],
}