1

I have a OP : {'2017-05-06': [3, 7, 8],'2017-05-07': [3, 9, 10],'2017-05-08': [4]}

from the OP I just want another OP :

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

which means:
Ncleand is 2017-05-06
element total is 18 so '2017-05-06': [3 -18, 7-18, 8-18] = '2017-05-06': [15, 11, 10]
likewise all elements data.
So final output is {'2017-05-06': [15, 11, 10],'2017-05-07': [19, 13, 12],'2017-05-08': [4]}

How to do this?

Note : I am using python 3.6.2 and pandas 0.22.0

code so far :

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)

From the cleand I want to do this?

4
  • 1
    Why doesn't [4] become [0]? The element total of [4] is 4, and 4 - 4 is 0. Commented Mar 26, 2018 at 9:03
  • if the array element size < 1 then leave as it is. Commented Mar 26, 2018 at 9:04
  • Also, please post your code. We expect help seekers to make an attempt to solve the problem, and show proof of that attempt. Commented Mar 26, 2018 at 9:05
  • @Aran-Fey check updated question Commented Mar 26, 2018 at 9:08

2 Answers 2

2

In a compact (but somehow inefficient) way:

>>> 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]}
Sign up to request clarification or add additional context in comments.

4 Comments

can we make if 2017-05-08': [0] then 2017-05-08': [1]? I mean make 1 where 0 in the element?
Sure. That would be: { x:[sum(y)-i for i in y] if len(y)>1 else [1] if y == [0] else y for x,y in op.items() }
This code needlessly computes the sum again each iteration over each of the lists. Don't try to cram everything into a comprehension or one-liner at the expense of readability and in this case, performance.
But where there is [0,0,0] then its printing [0,0,0] instead of [1,1,1]
2
def get_list_manipulation(list_):
    subtraction = list_
    if len(list_) != 1:
        total = sum(list_)
        subtraction = [total-val for val in list_]
    return subtraction

for key, values in data.items():
    data[key] = get_list_manipulation(values)
>>>{'2017-05-06': [15, 11, 10], '2017-05-07': [19, 13, 12], '2017-05-08': [4]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.