Home > Back-end >  group a a list of dictionaries in a dictionary by date, and get the highest value in each day
group a a list of dictionaries in a dictionary by date, and get the highest value in each day

Time:10-15

I have the following list of dictionaries:

[{'Reportdt': '2021-09-30',
  'Total_Vaccinations': 42107850,
  'Total_Individuals': None,
  'LastValue': 0,
  'ObjectId': 13452,
  'Elderly': 1655476,
  'FirstDose': 23386741,
  'SecondDose': 18721109},
 {'Reportdt': '2021-09-30',
  'Total_Vaccinations': 42107619,
  'Total_Individuals': None,
  'LastValue': 0,
  'ObjectId': 13451,
  'Elderly': 1655474,
  'FirstDose': 23386687,
  'SecondDose': 18720932},
 {'Reportdt': '2021-09-30',
  'Total_Vaccinations': 42107429,
  'Total_Individuals': None,
  'LastValue': 0,
  'ObjectId': 13450,
  'Elderly': 1655473,
  'FirstDose': 23386644,
  'SecondDose': 18720785},
 {'Reportdt': '2021-09-30',
  'Total_Vaccinations': 42107125,
  'Total_Individuals': None,
  'LastValue': 0,
  'ObjectId': 13449,
  'Elderly': 1655468,
  'FirstDose': 23386571,
  'SecondDose': 18720554},
 {'Reportdt': '2021-09-30',
  'Total_Vaccinations': 42106807,
  'Total_Individuals': None,
  'LastValue': 0,
  'ObjectId': 13448,
  'Elderly': 1655464,
  'FirstDose': 23386503,
  'SecondDose': 18720304}

since there are multiple dictionaries for each day, I'd like to get the highest Total_Vaccinations value for each single day.

I was able to group dictionaries by day and I was also able to get highest value for a specific key, however, I couldn't do both at the same time. Any suggestions? (I'm using Python)

CodePudding user response:

Try list.sort or sorted to sort the list of dictionaries. For example, you can use the below to sort by date (most recent first) and then by total vaccinations (highest to lowest).

from pprint import pprint

my_list = [{'Reportdt': '2021-09-30',
            'Total_Vaccinations': 42107850,
            'Total_Individuals': None,
            'LastValue': 0,
            'ObjectId': 13452,
            'Elderly': 1655476,
            'FirstDose': 23386741,
            'SecondDose': 18721109},
           {'Reportdt': '2021-09-30',
            'Total_Vaccinations': 42107619,
            'Total_Individuals': None,
            'LastValue': 0,
            'ObjectId': 13451,
            'Elderly': 1655474,
            'FirstDose': 23386687,
            'SecondDose': 18720932},
           {'Reportdt': '2021-09-30',
            'Total_Vaccinations': 42107429,
            'Total_Individuals': None,
            'LastValue': 0,
            'ObjectId': 13450,
            'Elderly': 1655473,
            'FirstDose': 23386644,
            'SecondDose': 18720785},
           {'Reportdt': '2021-09-30',
            'Total_Vaccinations': 42107125,
            'Total_Individuals': None,
            'LastValue': 0,
            'ObjectId': 13449,
            'Elderly': 1655468,
            'FirstDose': 23386571,
            'SecondDose': 18720554},
           {'Reportdt': '2021-09-30',
            'Total_Vaccinations': 42106807,
            'Total_Individuals': None,
            'LastValue': 0,
            'ObjectId': 13448,
            'Elderly': 1655464,
            'FirstDose': 23386503,
            'SecondDose': 18720304}]

my_list.sort(key=lambda x: (x['Reportdt'], x['Total_Vaccinations']),
             # Reverse to sort by highest -> lowest
             reverse=True)

print(f'-- List:')
pprint(my_list)

day_with_highest_vac = my_list[0]
print('-- Day with highest vaccination:')
pprint(day_with_highest_vac)

CodePudding user response:

If you could have multiple dictionaries for the same day, I think that this simple approach would do the job:

res = {}
for i in list_dictionaries:
   res[i['Reportdt']] = max(i['Total_Vaccinations'], res.get(i['Reportdt'], 0))

CodePudding user response:

if pandas is ok for you then

df=pd.DataFrame(my_list_of_dicts)
print(df.sort_values(['Reportdt', 'Total_Vaccinations'], ascending=False))
  • Related