Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:02:03+00:00 2026-06-16T05:02:03+00:00

I have a pandas dataframe like: In [61]: df = DataFrame(np.random.rand(3,4), index=[‘art’,’mcf’,’mesa’], columns=[‘pol1′,’pol2′,’pol3′,’pol4’]) In

  • 0

I have a pandas dataframe like:

In [61]: df = DataFrame(np.random.rand(3,4), index=['art','mcf','mesa'],
                        columns=['pol1','pol2','pol3','pol4'])

In [62]: df
Out[62]: 
          pol1      pol2      pol3      pol4
art   0.661592  0.479202  0.700451  0.345085
mcf   0.235517  0.665981  0.778774  0.610344
mesa  0.838396  0.035648  0.424047  0.866920

and I want to generate a row with the average for the policies across benchmarks and then plot it.

Currently, the way I do this is:

df = df.T
df['average'] = df.apply(average, axis=1)
df = df.T
df.plot(kind='bar')

Is there an elegant way to avoid the double transposition?

I tried:

df.append(DataFrame(df.apply(average)).T)
df.plot(kind='bar')

This will append the correct values but does not update the index properly and the graph is messed up.

A clarification. The result of the code with the double transposition is this: enter image description here
This is what I want. To show both the benchmarks and the average of the policies, not just the average. I was just curious if I can do it better.

Note that the legend is usually messed up. For a fix:

ax = df.plot(kind='bar')
ax.legend(patches, list(df.columns), loc='best')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:02:05+00:00Added an answer on June 16, 2026 at 5:02 am

    You can simply use the instance method mean of the DataFrame and than plot the results. There is no need for transposition.

    In [14]: df.mean()
    Out[14]: 
    pol1    0.578502
    pol2    0.393610
    pol3    0.634424
    pol4    0.607450
    
    In [15]: df.mean().plot(kind='bar')
    Out[15]: <matplotlib.axes.AxesSubplot at 0x4a327d0>
    

    policies.png

    Update

    If you want to plot the bars of all columns and the mean you can append the mean:

    In [95]: average = df.mean()
    
    In [96]: average.name = 'average'
    
    In [97]: df = df.append(average)
    
    In [98]: df
    Out[98]: 
                 pol1      pol2      pol3      pol4
    art      0.661592  0.479202  0.700451  0.345085
    mcf      0.235517  0.665981  0.778774  0.610344
    mesa     0.838396  0.035648  0.424047  0.866920
    average  0.578502  0.393610  0.634424  0.607450
    
    In [99]: df.plot(kind='bar')
    Out[99]: <matplotlib.axes.AxesSubplot at 0x52f4390>
    

    second plot

    If your layout doesn’t fit in to the subplot tight_layout will adjust the matplotlib parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a Pandas DataFrame df = DataFrame(index=['A','B','C'], columns=['x','y']) Now, I would like
Assume I have a pandas DataFrame with two columns, A and B. I'd like
I have a Pandas dataframe with columns like Order Balance Profit cum (%) I'm
I have a pandas DataFrame with a date column. It is not an index.
I have the following pandas Dataframe: from pandas import DataFrame, MultiIndex index = MultiIndex.from_tuples(zip([21,22,23],[45,45,46]),
I have a column in a pandas DataFrame that I would like to split
Suppose I have a Pandas dataframe df has columns a,b,c,d...z . And I want
When I have a pandas.DataFrame df with columns [A, B, C, D] , I
I have a dataframe of historical stock trades. The frame has columns like ['ticker',
I have a pandas dataframe object that looks like this: one two three four

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.