How to plot all named columns in a pandas multiindexed dataframe?

Viewed 41

I wrote a script to read a series of Excel using pandas.read_excel() into a DataFrame. Then these DataFrames are convert to MultiIndexed DataFrames. Their struture is as follows:

         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

How can I then plot all the the "Var2" data against the index, in both combined and singulated plots?

1 Answers

Use DataFrame.xs for select MultiIndex with DataFrame.plot:

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()
Related