将多索引pandas数据帧转换为单索引

2024-04-29 01:39:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧如下:

        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

它是一个以月份和类别桶为索引的多指标数据帧。我和q都是列

我是通过对下面这样的普通数据帧执行groupby操作得到的

^{pr2}$

我基本上想要一个数据帧,其中我有4列,每个列2个用于I_id,q-id用于两个月,一列用于category_bucket。所以基本上把上面的多索引数据帧转换成单索引,这样我就可以访问这些值了。在

目前,我很难获取某个月的i_id、q_id值和category值。在

如果您觉得有一种更简单的方法可以访问每个类别和月份的i_id和q_id值,而不必转换为单个索引,那也可以。在

不过,对于每个月和类别的组合,单个索引更容易循环到每个值中。在


Tags: or数据idbucketinstallation类别categoryalgebra