我有一个数据框架如下所示:
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它是一个以月份和类别桶为索引的多索引数据框架。和i_id,q_id作为列
我在普通数据框架上做了一个groupby操作,如下所示
invites_combined.groupby(['month', 'category_bucket'])[["i_id","q_id"]].count()我基本上想要一个数据框架,其中每个i_id有4列2列,两个月都有i_id,category_bucket有一列。所以基本上把上面的多索引数据帧转换成单一索引,这样我就可以访问这些值了。
目前,我很难访问特定月份的i_id、q_id和类别值。
如果您觉得有一种更容易的方法来访问每个类别和月份的i_id和q_id值,而不必转换为单个索引,这也很好。
但是,对于月份和类别的每个组合,单个索引更容易循环到每个值中。
发布于 2017-03-07 06:59:33
似乎您需要reset_index将MultiIndex转换为columns
df = df.reset_index()https://stackoverflow.com/questions/42642095
复制相似问题