2

I'm really new using Python. I need to achieve the following.

I have a list

[
  ['1604201722','16/04/2017','22', 100.0, 10.0, 110.0],<br>
  ['1604201722','16/04/2017','22', 100.0, 10.0, 110.0],<br>
  ['1604201719','16/04/2017','19', 100.0, 10.0, 110.0]<br>
]

Line structure is

['     ID   ','    DATE  ','Hour',  CANT, CANT, CANT]
['1604201722','16/04/2017','22'  ,  100.0,10.0, 110.0]

I need to accumulate the values like this:

['1604201722','16/04/2017', '22' , 200.0, 20.0, 240.0]
['1604201719','16/04/2017', '19' , 100.0, 10.0, 110.0]
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • The best way to approach your problem is to convert the list into a `pandas` dataframe and then `groupby(["ID","DATE","Hour"]).cum()`. I leave the implementation details to you. – DYZ Apr 16 '17 at 23:26

1 Answers1

1

Try using a pandas dataframe:

import pandas as pd
d = [
  ['1604201722','16/04/2017','22', 100.0, 10.0, 110.0],
  ['1604201722','16/04/2017','22', 100.0, 10.0, 110.0],
  ['1604201719','16/04/2017','19', 100.0, 10.0, 110.0]
]
df= pd.DataFrame(d,columns=['ID','DATE','HOUR','col1','col2','col3'])
print(df.groupby(['ID','DATE','HOUR']).sum())

Which will give this output:

ID         DATE       HOUR   col1  col2   col3                   
1604201719 16/04/2017 19    100.0  10.0  110.0
1604201722 16/04/2017 22    200.0  20.0  220.0
qbzenker
  • 4,412
  • 3
  • 16
  • 23