Created
September 11, 2017 22:19
-
-
Save 04pallav/2b3560b6f33e34152bd8192203f205f8 to your computer and use it in GitHub Desktop.
Flatten Heirarchical index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flattenHierarchicalCol(col,sep = '_'): | |
if not type(col) is tuple: | |
return col | |
else: | |
new_col = '' | |
for leveli,level in enumerate(col): | |
if not level == '': | |
if not leveli == 0: | |
new_col += sep | |
new_col += level | |
return new_col | |
ds1.columns = ds1.columns.map(flattenHierarchicalCol) | |
ds1.reset_index(inplace=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment