2

I thought it is simple, but I didn't find the solution. I have a data frame like this

df = pd.DataFrame({'month': [1, 4, 7, 10],
                'year': [2012, 2012, 2013, 2013],
                'sale': [55, 40, 84, 31]})
df.set_index('month')

I try to get a Multiindex with level 0 with 'year', and level 1 with 'month'. 'sale' will stay as column.

for example like this:

year  month sale 
2012  1     55
      4     40 
2013  7     84
      10    31

1 Answer 1

2

You can pass a list

df=df.set_index(['year','month'])
df
            sale
year month      
2012 1        55
     4        40
2013 7        84
     10       31
Sign up to request clarification or add additional context in comments.

1 Comment

this solution is correct! Thank you! in my original code i get 'NotImplementedError: > 1 ndim Categorical are not supported at this time'. month is 'int 64'. may I have to change the example...

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.