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
Alex
  • 999
  • 1
  • 14
  • 31

1 Answers1

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
BENY
  • 317,841
  • 20
  • 164
  • 234
  • 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... – Alex Dec 19 '19 at 22:10