在多索引数据框python pandas中转置一列

时间:2020-08-10 07:48:25

标签: python pandas

使用python 3.8,pandas 1.1.0

我有一个看起来像这样的数据框

ggplot(data, aes(x = group, y = 0, size = value)) +
  coord_polar() +
  geom_point()

我想转置工作日列以获得所需的df,如下所示:

                            weekday      count       sum 
location     week         
   A      2020-05-04        weekday       123         1
   A      2020-05-04        weekend       100         2
   A      2020-05-11        weekday       300         2
   A      2020-05-11        weekend       212         2
   B      2020-05-04        weekday        1          1
   B      2020-05-04        weekend        1          1
   B      2020-05-11        weekday        2          3
   B      2020-05-11        weekend        2          3

有没有办法做到这一点?

谢谢!

https://www.youtube.com/watch?v=kJsiiPK5sxs清楚地说明了枢轴,堆叠和拆堆。

我只需使用 weekday weekend weekday weekend count sum location week A 2020-05-04 123 100 1 2 A 2020-05-11 300 212 2 2 B 2020-05-04 1 1 1 1 B 2020-05-11 2 2 3 3

就可以得到想要的输出。

0 个答案:

没有答案