0

I the following pandas dataframe:

import pandas as pd

df = pd.DataFrame({'Year': [2023, 2024, 2022,2022, 2023,2025,2024,2025,2022,2023],'Name': ['John', 'Jim', 'John','Jim', 'John','Jim','John','Jim','John','Jim']
                  ,'Month': ['Jan', 'Jan', 'Feb','Jan', 'Jan','Feb','Jan','Jan','Feb','Feb'],'Day': [1, 2, 5,6, 7,4,8,24,13,15]})
df

which looks like:

enter image description here

I am trying to convert this to a dictionary/api format so it can be use in a get request.

I need it in a format that is grouped by Name, then Year, then Month and the values are Day. My front end output expected is this

enter image description here

By any help to create the dictionary/api view to achieve this would be fantastic. Thanks very much

SOK
  • 1,732
  • 2
  • 15
  • 33
  • 1
    What have you tried and what specific problem you have? – buran Feb 26 '23 at 05:33
  • I don't know how to convert so i was hoping someone could help point me in the right direction with a solution – SOK Feb 26 '23 at 05:43
  • 1
    You can use [DataFrame.pivot](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.pivot.html) or [DataFrame.pivot_table](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.pivot_table.html) or [DataFrame.groupby](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html) – buran Feb 26 '23 at 05:48
  • Also, [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/4046632) – buran Feb 26 '23 at 05:50

0 Answers0