Assume a simple dataframe like:
df = pd.DataFrame({'A' : [1], 'B' : [0], 'value1' : [33], 'value2' : [23]})
df
A B value1 value2
0 1 0 33 23
How can I assign superordinated column names s.t. I get a dataframe like:
values
A B value1 value2
0 1 0 33 23
This should be somehow possible using multiindexing, but I can't figure out how.
Pay attention to that A
and B
have no joined heading. Therefore
df.columns = pd.MultiIndex.from_product([['values'], df.columns])
is not enough. Hence it is not a duplicate.
I also tried:
df[['value1','value2']].columns = pd.MultiIndex.from_product([['values'], df[['value1','value2']].columns])
But unfortunately it does not assign the new column name to the original dataframe.