0

I have a multiindex pandas dataframe that looks like this:

            cis-aconitate  cis-aconitate +1  ...  cis-aconitate +5  cis-aconitate +6
     GROUP                                   ...                                    
mean ADE         1.481639          0.696184  ...          0.193380          0.018597
     VEH         1.000000          1.000000  ...          1.000000          1.000000
std  ADE         0.307211          0.209418  ...          0.114939          0.020461
     VEH         0.573162          0.412895  ...          0.384928          0.752999

With indexing like this:

MultiIndex([('mean', 'ADE'),
            ('mean', 'VEH'),
            ( 'std', 'ADE'),
            ( 'std', 'VEH')],
           names=[None, 'GROUP'])

I am trying to create a barplot of the mean values on the y-axis. Using df.unstack() and then df.plot.bar() I can get this: enter image description here But what I would ultimately like is to have the means only, with 'cis-aconitate, cis-aconitate +1 ...' across the x-axis, grouped into ADE and VEH (one color each) and then 'std' values used for the error bars. This is proving surprisingly tricky. Can anyone help? Thanks in advance!

1 Answer 1

1

To avoid doing a lot of transformations to the data frame and based on this example, the solution I found was to split the means from the errors using cross section.

import pandas as pd

# Example data
index = pd.MultiIndex.from_tuples([('mean', 'ADE'), ('mean', 'VEH'), ( 'std', 'ADE'), ('std', 'VEH')], names=[None, 'GROUP'])

df = pd.DataFrame({'cis-aconitate': [1.5, 1, 0.3, 0.2], 'cis-aconitate +1': [0.7, 1, 0.2, 0.4]}, index=index)
df

# Split dataframe into means and errors
means = df.xs('mean').transpose()
errors = df.xs('std').transpose()

means.plot.bar(yerr=errors)

Sign up to request clarification or add additional context in comments.

2 Comments

If you have access to the data that originated this data frame, seaborn can also be a nice alternative, as you'll probably only need to melt the data and have a 'group' column. It plots error bars as confidence intervals by default, but you can specify standard deviation.
This worked brilliantly! Thank you! Cross section with transpose was the key. I will definitely explore using seaborn with the long form data too. Will likely lead to more elegant code. Thanks very much for you help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.