I have a dataframe looks like this:
A B C
Date
data data data data
data data data data
how can I add header to make it looks like this:
All Data
Type A B C
Date
data data data data
data data data data
I have a dataframe looks like this:
A B C
Date
data data data data
data data data data
how can I add header to make it looks like this:
All Data
Type A B C
Date
data data data data
data data data data
A simple way would be using concat()
:
df=pd.concat({'All Data':df},axis=1,names=[None,'Type'])
OR
use pd.MultiIndex.from_product()
:
df.columns=pd.MultiIndex.from_product([['All Data'],df.columns],names=[None,'Type'])
OR
use pd.MultiIndex.from_arrays()
df.columns=pd.MultiIndex.from_arrays([['All Data']*len(df.columns),df.columns],names=[None,'Type'])
output of df
:
All Data
Type A B C
Date
data data data data
data data data data
Update: If you want to show the header in middle then use style.set_table_styles()
:
df=df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
output of df
:
All Data
Type A B C
Date
data data data data
data data data data