Bharath_Raja
Bharath_Raja

Reputation: 642

Can I get an extra header in pandas as a name on top of all the columns

I want to add a title for a dataframe as header. For suppose this is the dataframe.

         count    mean    std   min   25%   50%    75%   max
 Gender                                                     
 F         4.0  86.250  3.862  82.0  83.5  86.5  89.25  90.0
 M         6.0  85.833  5.565  78.0  82.0  86.5  90.25  92.0

I want to get it with some title in the top header or row like this.

                                Analysis
         count    mean    std   min   25%   50%    75%   max
 Gender                                                     
 F         4.0  86.250  3.862  82.0  83.5  86.5  89.25  90.0
 M         6.0  85.833  5.565  78.0  82.0  86.5  90.25  92.0

Upvotes: 2

Views: 161

Answers (1)

jezrael
jezrael

Reputation: 862611

Close, what you need is MultiIndex, but it is not in the middle but left:

df.columns = pd.MultiIndex.from_product([['Analysis'], df.columns])
print (df)
       Analysis                                              
          count    mean    std   min   25%   50%    75%   max
Gender                                                       
F           4.0  86.250  3.862  82.0  83.5  86.5  89.25  90.0
M           6.0  85.833  5.565  78.0  82.0  86.5  90.25  92.0

Upvotes: 2

Related Questions