Skip to content Skip to sidebar Skip to footer

Apply Hierarchy Or Multi-index To Pandas Columns

I have seen lots of examples on how to arrange dataframe row indexes hierarchically, but I am trying to do the same for columns and am not understanding the syntax: I am reading th

Solution 1:

Here is a quick-fix solution for you:

data = pd.read_csv('data.csv')
>>>arrays = [[ '', 'Subject1', 'Subject1', 'Subject2', 'Subject2'], data.columns]>>>df = pd.DataFrame(data.values, columns=arrays)>>>print df
        Subject1        Subject2      
   rno     mark1  lab1     mark2  lab2
0    1        78    45        34    54
1    2        23    54        87    46

[2 rows x 5 columns]

Just another way to do the same:

>>>data = pd.read_csv('data.csv')>>>data_pieces = [data.ix[:, [0]], data.ix[:, [1, 2]], data.ix[:, [3,4]]]>>>data = pd.concat(data_pieces, axis=1, keys=['','Subject1', 'Subject2'])>>>print data
        Subject1        Subject2      
   rno     mark1  lab1     mark2  lab2
0    1        78    45        34    54
1    2        23    54        87    46

[2 rows x 5 columns]

Post a Comment for "Apply Hierarchy Or Multi-index To Pandas Columns"