python - 数组元素中的聚合 - python

标签 python

我有一个 OP:{'2017-05-06': [3, 7, 8],'2017-05-07': [3, 9, 10],'2017-05-08 ': [4]}

从 OP 我只想要另一个 OP :

{'2017-05-06': [15, 11, 10], '2017-05-07': [19, 13, 12], '2017-05-08': [4]

这意味着:
Ncleand2017-05-06
元素总计18 所以'2017-05-06': [3 -18, 7-18, 8-18] = '2017-05-06 ': [15, 11, 10]
同样所有元素数据。
所以最终输出是 {'2017-05-06': [15, 11, 10],'2017-05-07': [19, 13, 12],'2017-05-08': [4 ]}

如何做到这一点?

注意:我使用的是 python 3.6.2 和 pandas 0.22.0

到目前为止的代码:

import pandas as pd
dfs = pd.read_excel('ff2.xlsx', sheet_name=None)
dfs1 = {i:x.groupby(pd.to_datetime(x['date']).dt.strftime('%Y-%m-%d'))['duration'].sum() for i, x in dfs.items()}
d = pd.concat(dfs1).groupby(level=1).apply(list).to_dict()
actuald = pd.concat(dfs1).div(80).astype(int)
sum1 = actuald.groupby(level=1).transform('sum')
m = actuald.groupby(level=1).transform('size') > 1
cleand = sum1.sub(actuald).where(m, actuald).groupby(level=1).apply(list).to_dict()
print (cleand)

从干净的我想做这个?

最佳答案

以紧凑(但不知何故效率低下)的方式:

>>> op = {'2017-05-06': [3, 7, 8],'2017-05-07': [3, 9, 10],'2017-05-08': [4]}
>>> { x:[sum(y)-i for i in y] if len(y)>1 else y for x,y in op.items() }

#output:
{'2017-05-06': [15, 11, 10], '2017-05-07': [19, 13, 12], '2017-05-08': [4]}

关于python - 数组元素中的聚合 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49487607/

相关文章:

python - 从一个输入解包值以进行整数处理?

python - Yum 在 Amazon Linux 上安装 libhdf5-dev

python - django-social-auth - 缺少 HTTPSConnection

python - selenium:如何在Mac上加载本地html文件?

python - Figsize 不适用于 matplotlib 3d 绘图

python - 使用 pandas 添加带有 "to_csv"的评论

python - 如何从 Python 中的不同类访问使用一个类创建的对象

python - python 中的正则表达式

Python:局部变量神秘地更新全局变量

python - 如何使用 selenium python 动态单击加载按钮?