如何在Pandas多级索引数据框中绘制所有命名列?

4

我编写了一个脚本,使用pandas.read_excel()从Excel文件中读取一系列数据,并将这些DataFrames转换为多重索引DataFrame。它们的结构如下:

         Customer1               Customer2              Customer3
     Var1   Var2   Var3      Var1   Var2   Var3     Var1   Var2   Var3 
0     1       6     4         6      2      5         7      6      6
1     3       5     0         1      5      4         1      6      5
2     5       7     3         6      1      4         7      5      1
.     .       .     .         .      .      .         .      .      .
.     .       .     .         .      .      .         .      .      .
100   1       6     4         6      2      4         7      6      6

我该如何绘制所有"Var2"数据与索引的图表,包括组合和单个的图表?
1个回答

4

使用DataFrame.xs选择具有DataFrame.plotMultiIndex

print (df.xs('Var2', axis=1, level=1))
     Customer1  Customer2  Customer3
0            6          2          6
1            5          5          6
2            7          1          5
100          6          2          6

df.xs('Var2', axis=1, level=1).plot()

哦,哇。感谢您的答案!我得更多地了解一下什么是轴和级别,以及是否可以用名称来标识它们。 - Wen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接