How To 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: Given: df = pd.
Solution 1:
You can define MultiIndices
using the from_arrays
or from_tuples
or from_product
classmethods. Here is an example using from_arrays
:
arrays = [[1, 2]*3, ['A', 'B', 'C']*2]
columns = pd.MultiIndex.from_arrays(arrays, names=['foo', 'bar'])
df = pd.DataFrame(np.random.randn(2,6),
columns=columns,
index= pd.date_range('20000103',periods=2))
yields
In[81]: dfOut[81]:
foo121212barABCABC2000-01-031.277234-0.8995470.040337-0.878752-0.5243360.9224402000-01-04-1.7067970.4503791.510868-2.539827-1.909996-0.003851
Defining a MultiIndex for the index is done in exactly the same way as for columns.
Post a Comment for "How To Apply Hierarchy Or Multi-index To Pandas Columns"