Skip to content

Instantly share code, notes, and snippets.

@04pallav
Created September 11, 2017 22:19
Show Gist options
  • Save 04pallav/2b3560b6f33e34152bd8192203f205f8 to your computer and use it in GitHub Desktop.
Save 04pallav/2b3560b6f33e34152bd8192203f205f8 to your computer and use it in GitHub Desktop.
Flatten Heirarchical index
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