简体   繁体   中英

Converting a multi-index pandas dataframe to single index

I have a data frame like below:

        i_id    q_id
month   category_bucket     
Aug Algebra Tutoring    187 64
Balloon Artistry    459 401
Carpet Installation or Replacement  427 243
Dance Lessons   181 46
Landscaping 166 60
Others  9344    4987
Tennis Instruction  161 61
Tree and Shrub Service  383 269
Wedding Photography 161 49
Window Repair   140 80
Wiring  439 206
July    Algebra Tutoring    555 222
Balloon Artistry    229 202
Carpet Installation or Replacement  140 106
Dance Lessons   354 115
Landscaping 511 243
Others  9019    4470
Tennis Instruction  613 324
Tree and Shrub Service  130 100
Wedding Photography 425 191
Window Repair   444 282
Wiring  154 98

It's a multi-index data frame with month and category bucket as index. And i_id, q_id as columns

I got this by doing a groupby operation on a normal data frame like below

invites_combined.groupby(['month', 'category_bucket'])[["i_id","q_id"]].count()

I basically want a data frame where I have 4 columns 2 each for i_id, q-Id for both the months and a column for category_bucket. So basically converting the above multi-index data frame to single index so that I can access the values.

Currently it's difficult for me to access the values of i_id, q_id along for a particular month and category value.

If you feel there is an easier way to access the i_id and q_id values for each category and month without having to convert to single index that is fine too.

Single index would be easier to loop into each value for each combination of month and category though.

看来您需要reset_index才能将MultiIndex转换为columns

df = df.reset_index()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM