-2

input={1:5,2:8,9:3,11:4,18:3,21:4,3:8,350:5}

and I would like to do the group by operation and perform the sum operation on

the value of that dictionary.

like grp_1=1,11,21 ; grp_2=9,3,18 ; grp_3= 2

and output should like as below

grp_1= 13 (5+8+8 dict values)

grp_2= 14 (3+4+3 dict values)

grp_3= 13

please suggest the best way to do that and then I should have the option to

customize the groups so that I can add another number in the future...

  • 1
    Are you required to do it in python? If not, you can do it directly in excel with `COUNTIF` function. See example from microsoft, https://support.office.com/en-us/article/countif-function-e0de10c6-f885-4e71-abb4-1f464816df34. – MaxPlankton Aug 22 '18 at 10:40

1 Answers1

0

First off:

Use python 3.x not python 2.x as python 2 is dying out: https://pythonclock.org/

In python 3 it is best to use the CSV library by using:

import csv

and then read in the values you have.

Then you can iterative over each row and populate the values you want in a dictionary:

region = {}

Each time you iterator over each line of your dataset you either update the dictionary when you find a new region or you just add one if it already exists.

if currentRegion in region.keys():
  region[currentRegion]+=1
else:
  region[currentRegion]=1

If you don't have the skills yet to use python or don't understand the lines of code above, its a good idea to look at the python 3.x tutorial. Its about 100 pages long but you only need to cover until chapter 5 till you have covered the use of lists and dictionaries to be able to code up the type of problem you describe above. The tutorial allows a novice to learn python basic skills within 1/2-1 day.

Eamonn Kenny
  • 1,926
  • 18
  • 20