1

I'm working with a music database and I have all the data transformed the way I want it to except I cannot yet figure out how to add another level of indexing to my dataframe. So far, I've grouped everything on genre and aggregated a few computations and that came out fine:

current dataframe

Now I'd like for my dataframe to look like this dataframe where the major_category is genre, the minor_category is all the columns except duration, and the values are the ones associated to the respective minor_category:

desired dataframe

Any help is much appreciated!

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
big_cactus
  • 69
  • 1
  • 6
  • 2
    look at `pd.melt`, set `ignore_index=False`. Also, it is better to share a small reproducible sample of your data, than pics – sammywemmy Jul 17 '21 at 23:45
  • There is a good example here: https://stackoverflow.com/questions/28654047/pandas-convert-some-columns-into-rows – William Jul 17 '21 at 23:46
  • It's just a guess because I don't have a frame work with but something like: `df.set_index('duration', append=True).stack()` – Henry Ecker Jul 17 '21 at 23:53
  • 1
    Nicely represented, but as @sammywemmy says, you should copy paste the raw data and desired output as text. Why? (a) it's easier to read; (b) people wanting to help you can copy paste the text into their code and verify that their advice works. – John D Jul 17 '21 at 23:55
  • Sorry I could have included more code - the data is privately accessed so I was hesitant to share. Basically, I grouped on `genre` and `pd.agg()`'d on the columns I wanted leaving me this. – big_cactus Jul 18 '21 at 18:02

0 Answers0