0

I unstacked a dataframe, and now have a two-level column hierarchy. I unstacked the original dataframe using a integer ids 1, 2, 3 in terms of pure columns, there are now 3 times as many columns as before.

I am pretty new to pandas and would prefer to deal with this in one level, by flattening this two level schema into one level, where the unique combination of column labels in the hierarchical schema are the unique column labels in the one level schema.

Currently I would get a certain column by saying df['columnName'][1] or df['columnName'][2], or df['otherColumnName'][1].

I want to be able to get the column by querying the string df['columnName-1'] df['columnName-2'] or df['otherColumnName-1'] or something similar to that.

The closest thing I've found is get_level_values() but it doesn't let me modify the column name in the way I just described. I am struggling to phrase this search in google, if you have advice on how to even state this question succinctly, that would also be helpful.

  • 1
    here's a function that will help https://github.com/International-BMP-Database/wqio/blob/master/wqio/utils/misc.py#L103 – Paul H Sep 10 '21 at 03:25
  • If I'm understanding correctly. `df.columns = [f'{f}-{s}' for f, s in df.columns]` from [this answer](https://stackoverflow.com/a/51735628/15497888) – Henry Ecker Sep 10 '21 at 03:25

0 Answers0