0

I have this data frame in pandas.

course = Courses.objects.filter(date__date__gte=dateFrom.date(),date__date__lte=dateTo.date()).values('course','price','date')
df_course = pd.DataFrame(course)
df_course['day_of_year'] = df_course.formatted_date.apply(lambda x: x.dayofyear)
dailyMinPrices = df_course.groupby(['hotel', 'day_of_year'])['price'].min()

It return:

 Course    day_of_year     Price
 Course 1  154             70.0
           155             74.0
           156             85.0
           157             90.0
           158             83.0
           159             79.0
           160             81.0
           161             75.0
           162            113.0
           163             85.0
           164             83.0
 Course 2  154             96.0
           155            112.0
           156             73.0
           157             78.0
 Course 3  154             99.0
           155             74.0
           156             78.0
           157             70.0
           158             94.0
           159             82.0

I am trying to convert it to django list or django dict, but It always return:

  "dailyMinPrices": [70.0,74.0,85.0,90.0,83.0, 79.0,81.0,75.0,113.0,85.0]

I am trying to get this estructure:

  "dailyMinPrices": {'course 1':{['day_of_year':154,'price':70.0], ['day_of_year':154,'price':70.0],['day_of_year':154,'price':70.0]}, 'course 2':{...},'course 3':{...}}

I am trying with:

 dailyMinPrices.values.tolist() #Not working
 dailyMinPrices.to_dict(), Return errors

I would like to do it without for, because it is a lot of registers.

2
  • Can you reset the index and then try? Commented Jun 16, 2022 at 5:59
  • Only like this? dailyMinPrices.reset_index() , and after dailyMinPrices.to_dict() ? Commented Jun 16, 2022 at 6:25

1 Answer 1

1

to_dict(orient='records') is close to the expected result. But as you want a hierarchical dict, you should first use a groupby to split the dataframe on the first level of index. With your sample data,

{i[0]: i[1].reset_index(level=1).to_dict(orient='records')
    for i in df.groupby(level=0)}

gives as expected (using pprint):

{'Course 1': [{'Price': 70.0, 'day_of_year': 154},
              {'Price': 74.0, 'day_of_year': 155},
              {'Price': 85.0, 'day_of_year': 156},
              {'Price': 90.0, 'day_of_year': 157},
              {'Price': 83.0, 'day_of_year': 158},
              {'Price': 79.0, 'day_of_year': 159},
              {'Price': 81.0, 'day_of_year': 160},
              {'Price': 75.0, 'day_of_year': 161},
              {'Price': 113.0, 'day_of_year': 162},
              {'Price': 85.0, 'day_of_year': 163},
              {'Price': 83.0, 'day_of_year': 164}],
 'Course 2': [{'Price': 96.0, 'day_of_year': 154},
              {'Price': 112.0, 'day_of_year': 155},
              {'Price': 73.0, 'day_of_year': 156},
              {'Price': 78.0, 'day_of_year': 157}],
 'Course 3': [{'Price': 99.0, 'day_of_year': 154},
              {'Price': 74.0, 'day_of_year': 155},
              {'Price': 78.0, 'day_of_year': 156},
              {'Price': 70.0, 'day_of_year': 157},
              {'Price': 94.0, 'day_of_year': 158},
              {'Price': 82.0, 'day_of_year': 159}]}
Sign up to request clarification or add additional context in comments.

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.