Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot specific rows and columns of pandas dataframe (based on name of row and name of column) in bar plot with error bars?

I have data in a csv file structured like so:

    Subject    group    Result1    Result2...    ResultN
    101        a        .5         .1            .2
    103        b        .1         .2            .5
    104        b        .2         .3            .4
    mean_a     a        .5         .1            .2
    mean_b     b        .1         .6            .4
    ste_a      a        .05        .02           .03
    ste_b      b        .01        .05           .04

I just want to end up with a bar plot, grouped by the Result, of the mean rows' values for each group, with the stes as the error bars. Unfortunately, I'm having trouble doing so. I can convert the dataframe to two separate dataframes, one for means and one for stes, like this:

               a        b
    Result1    .5       .1
    Result2    .1       .6

However, I cannot figure out how to plot the second dataframe of stes as error bars, and my method seems overly complicated, so I was wondering if anyone knows of a simpler way to do this and, if not, how to use the one dataframe to plot error bars for the other dataframe.

like image 648
Taylor Avatar asked Oct 01 '22 01:10

Taylor


1 Answers

It is quite easy, just pass your error data to yerr argument the same as you will do in matplotlib.

DF=pd.DataFrame({'a':[.5,.1],'b':[.1,.6]})
DF.index=['Result1','Result2']
DF.plot(kind='bar',yerr=DF.b)

enter image description here

like image 168
CT Zhu Avatar answered Oct 07 '22 21:10

CT Zhu