0

I am attempting to use list comprehension in order to condense two tiers of column index into one.

My data currently looks as follows:

      Value
-----+-----------
Year |Jan|Feb|Mar
-----+---+---+---
2010 | 1 | 2 | 3
2011 | 1 | 2 | 6
2012 | 3 | 4 | 1

Note that this is a condensed version for visualisation purpose and all months are represented in the true dataset. My goal is to change the formatting of the table so that I can have single column titles made up of both tiers at present. I think list comprehension is the best way to do this but am having trouble working out the logistics.

Ideally the headers for the columns will read Value_Jan, Value_Feb etc. once the transformation is complete.

Currently my code reads as follows:

cols = pd.MultiIndex.from_tuples[("Value", "April"),
                                  ("Value", "August"), 
                                  ("Value", "December"),
                                  ("Value", "February"),
                                  ("Value", "January"),
                                  ("Value", "July"),
                                  ("Value", "June"),
                                  ("Value", "March"),
                                  ("Value", "May"),
                                  ("Value", "November"),
                                  ("Value", "October"),
                                  ("Value", "September")]
df = pd.df(columns=cols)
df.head()

When this code is run I get the following error:

TypeError: 'method' object is not subscriptable

Any better ways to go about this or troubleshooting help would be greatly appreciated!

Niamh
  • 51
  • 5

0 Answers0