Thursday, September 10, 2020

Pandas Dataframe to Python Dictionary

Assuming we have this table of "Confirmed COVID-19 Cases in Nigeria" as seen below and we want to convert either the whole table or some of its columns to a python dictionary. If you don't know already, a python dictionary is a data structure that consists of a collection of key-value pairs.



The column are as follow:-

  1. Column A: States Affected
  2. Column B: No. of Cases (Lab Confirmed)
  3. Column C: No. of Cases (on admission)
  4. Column D: No. Discharged
  5. Column E: No. of Deaths

Convert the whole table into dictionary

This is useful when you want to have a data structure where the column names are the keys and their respective contents are the values. This can be achieved using the pandas .to_dict function.

mydict = df.to_dict('dict')
print(mydict)

{'No. Discharged': {0: '15,233',
  1: '1,624',
  2: '1,961',
  3: '1,785',
  4: '2,404',
  5: '2,099',
  6: '2,067',
  7: '1,603',
  8: '1,604',
  9: '1,624',
  10: '1,398',
  11: '1,088',
  12: '986',
  13: '794',
  14: '457',
  15: '712',
  16: '754',
  17: '643',
  18: '703',
  19: '644',
  20: '211',
  21: '291',
  22: '298',
  23: '363',
  24: '308',
  25: '253',
  26: '242',
  27: '216',
  28: '180',
  29: '181',
  30: '142',
  31: '82',
  32: '73',
  33: '73',
  34: '73',
  35: '59',
  36: '3'},
 'No. of Cases (Lab Confirmed)': {0: '18,389',
  1: '5,310',
  2: '3,194',
  3: '2,745',
  4: '2,600',
  5: '2,214',
  6: '2,191',
  7: '1,768',
  8: '1,728',
  9: '1,687',
  10: '1,561',
  11: '1,184',
  12: '1,030',
  13: '982',
  14: '819',
  15: '816',
  16: '802',
  17: '744',
  18: '741',
  19: '670',
  20: '534',
  21: '460',
  22: '438',
  23: '391',
  24: '322',
  25: '287',
  26: '282',
  27: '244',
  28: '228',
  29: '223',
  30: '159',
  31: '93',
  32: '91',
  33: '83',
  34: '78',
  35: '67',
  36: '5'},
 'No. of Cases (on admission)': {0: '2,954',
  1: '3,619',
  2: '1,195',
  3: '930',
  4: '96',
  5: '83',
  6: '66',
  7: '117',
  8: '70',
  9: '37',
  10: '131',
  11: '75',
  12: '14',
  13: '163',
  14: '338',
  15: '96',
  16: '31',
  17: '78',
  18: '2',
  19: '12',
  20: '312',
  21: '160',
  22: '128',
  23: '7',
  24: '3',
  25: '29',
  26: '32',
  27: '16',
  28: '33',
  29: '23',
  30: '0',
  31: '3',
  32: '13',
  33: '2',
  34: '0',
  35: '0',
  36: '0'},
 'No. of Deaths': {0: 202,
  1: 67,
  2: 38,
  3: 30,
  4: 100,
  5: 32,
  6: 58,
  7: 48,
  8: 54,
  9: 26,
  10: 32,
  11: 21,
  12: 30,
  13: 25,
  14: 24,
  15: 8,
  16: 17,
  17: 23,
  18: 36,
  19: 14,
  20: 11,
  21: 9,
  22: 12,
  23: 21,
  24: 11,
  25: 5,
  26: 8,
  27: 12,
  28: 15,
  29: 19,
  30: 17,
  31: 8,
  32: 5,
  33: 8,
  34: 5,
  35: 8,
  36: 2},
 'States Affected': {0: 'Lagos',
  1: 'FCT',
  2: 'Oyo',
  3: 'Plateau',
  4: 'Edo',
  5: 'Kaduna',
  6: 'Rivers',
  7: 'Delta',
  8: 'Kano',
  9: 'Ogun',
  10: 'Ondo',
  11: 'Enugu',
  12: 'Ebonyi',
  13: 'Kwara',
  14: 'Katsina',
  15: 'Abia',
  16: 'Osun',
  17: 'Gombe',
  18: 'Borno',
  19: 'Bauchi',
  20: 'Imo',
  21: 'Benue',
  22: 'Nasarawa',
  23: 'Bayelsa',
  24: 'Jigawa',
  25: 'Ekiti',
  26: 'Akwa Ibom',
  27: 'Niger',
  28: 'Adamawa',
  29: 'Anambra',
  30: 'Sokoto',
  31: 'Kebbi',
  32: 'Taraba',
  33: 'Cross River',
  34: 'Zamfara',
  35: 'Yobe',
  36: 'Kogi'}}




 Convert some columns into dictionary

If we want to pair two columns as keys and values, then this is the option to use. Here we use the dict() and zip() methods.

Assuming we waat "Column A: States Affected" as keys and "Column B: No. of Cases (Lab Confirmed)" as values, the syntax will look like this:-

mydict = dict(zip(df['States Affected'], df['No. of Cases (Lab Confirmed)']))
print(mydict)

{'Abia': '816',
 'Adamawa': '228',
 'Akwa Ibom': '282',
 'Anambra': '223',
 'Bauchi': '670',
 'Bayelsa': '391',
 'Benue': '460',
 'Borno': '741',
 'Cross River': '83',
 'Delta': '1,768',
 'Ebonyi': '1,030',
 'Edo': '2,600',
 'Ekiti': '287',
 'Enugu': '1,184',
 'FCT': '5,310',
 'Gombe': '744',
 'Imo': '534',
 'Jigawa': '322',
 'Kaduna': '2,214',
 'Kano': '1,728',
 'Katsina': '819',
 'Kebbi': '93',
 'Kogi': '5',
 'Kwara': '982',
 'Lagos': '18,389',
 'Nasarawa': '438',
 'Niger': '244',
 'Ogun': '1,687',
 'Ondo': '1,561',
 'Osun': '802',
 'Oyo': '3,194',
 'Plateau': '2,745',
 'Rivers': '2,191',
 'Sokoto': '159',
 'Taraba': '91',
 'Yobe': '67',
 'Zamfara': '78'}


Happy coding!

No comments:

Post a Comment