Tutorial by Examples

Given the following DataFrame: In [11]: df = pd.DataFrame(np.random.randn(6, 3), columns=['A', 'B', 'C']) In [12]: df.set_index(['A', 'B'], inplace=True) In [13]: df Out[13]: C A B 0.902764 -0.259656 -1.864541 -0.695893 0.308893 0...
Given the following DataFrame: In [11]: df = pd.DataFrame({'a':[1,1,1,2,2,3],'b':[4,4,5,5,6,7,],'c':[10,11,12,13,14,15]}) In [12]: df.set_index(['a','b'], inplace=True) In [13]: df Out[13]: c a b 1 4 10 4 11 5 12 2 5 13 6 14 3 7 15 You can iterate by any lev...
This example shows how to use column data to set a MultiIndex in a pandas.DataFrame. In [1]: df = pd.DataFrame([['one', 'A', 100], ['two', 'A', 101], ['three', 'A', 102], ...: ['one', 'B', 103], ['two', 'B', 104], ['three', 'B', 105]], ...: columns=['c1'...
Given a DataFrame with MultiIndex columns # build an example DataFrame midx = pd.MultiIndex(levels=[['zero', 'one'], ['x','y']], labels=[[1,1,0,],[1,0,1,]]) df = pd.DataFrame(np.random.randn(2,3), columns=midx) In [2]: df Out[2]: one zero y x ...
Start with a standard DataFrame df = pd.DataFrame(np.random.randn(2,3), columns=['a','b','c']) In [91]: df Out[91]: a b c 0 -0.911752 -1.405419 -0.978419 1 0.603888 -1.187064 -0.035883 Now to change to MultiIndex, create a MultiIndex object and assign it to df....
MultiIndex can also be used to create DataFrames with multilevel columns. Just use the columns keyword in the DataFrame command. midx = pd.MultiIndex(levels=[['zero', 'one'], ['x','y']], labels=[[1,1,0,],[1,0,1,]]) df = pd.DataFrame(np.random.randn(6,4), columns=midx) In [86]: df Out[86]: ...
To view all elements in the index change the print options that “sparsifies” the display of the MultiIndex. pd.set_option('display.multi_sparse', False) df.groupby(['A','B']).mean() # Output: # C # A B # a 1 107 # a 2 102 # a 3 115 # b 5 92 # b 8 98 # c 2 87 # c 4 104 #...

Page 1 of 1