0

I would like to create a stacked bar plot from the following dataframe:

   VALUE     COUNT  RECL_LCC  RECL_PI
0      1  15686114         3        1
1      2  27537963         1        1
2      3  23448904         1        2
3      4   1213184         1        3
4      5  14185448         3        2
5      6  13064600         3        3
6      7  27043180         2        2
7      8  11732405         2        1
8      9  14773871         2        3

There would be 2 bars in the plot. One for RECL_LCC and other for RECL_PI. There would be 3 sections in each bar corresponding to the unique values in RECL_LCC and RECL_PI i.e 1,2,3 and would sum up the COUNT for each section. So far, I have something like this:

df = df.convert_objects(convert_numeric=True)    
sub_df = df.groupby(['RECL_LCC','RECL_PI'])['COUNT'].sum().unstack()
sub_df.plot(kind='bar',stacked=True)

However, I get this plot:

enter image description here

Any idea on how to obtain 2 columns (RECL_LCC and RECL_PI) instead of these 3?

9
  • 2
    Are all your dtypes numeric? You can do df = df.convert_objects(convert_numeric=True) to ensure the dtypes are ints/floats Commented Oct 31, 2014 at 20:56
  • Agree with @EdChum. Your code checks out on my side (I can plot it just fine). Commented Oct 31, 2014 at 20:56
  • thanks! that was needed, modified code to include the convert_objects statement. the plot still needs some work. Commented Oct 31, 2014 at 21:01
  • Should I post an answer? Commented Oct 31, 2014 at 21:02
  • Does your answer include fix for the plot too? Commented Oct 31, 2014 at 21:03

1 Answer 1

2

So your problem was that the dtypes were not numeric so no aggregation function will work as they were strings, so you can convert each offending column like so:

df['col'] = df['col'].astype(int)

or just call convert_objects on the df:

df.convert_objects(convert_numeric=True)
Sign up to request clarification or add additional context in comments.

6 Comments

thanks! How did you know that they were not numeric?
In your original post you had the error: *** TypeError: Empty 'DataFrame': no numeric data to plot now the only way you could have an empty dataframe after performing the aggregation is if none of your columns were numeric
gotcha, makes sense now
I don't know how you created the df but usually you can specify the dtype when loading data or when creating the df
I was reading it using from_csv, that does not have a dtype, it seems like
|

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.