Creating a tricky barrier in pandas

I would like to create a line-by-line graph 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 were 2 bars in the plot. One for RECL_LCC and one for RECL_PI. Each bar will have 3 sections, corresponding to unique values ​​in RECL_LCC and RECL_PI ie 1,2,3, and summarize 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 am getting this plot: enter image description here

Any idea on how to fix it? I am doing something wrong with groupby but not sure about the solution

+3


source to share


1 answer


I put the data shown in stackpandas.dat

. Given this data:

from pandas import *
import matplotlib.pyplot as plt

df = read_table("stackpandas.dat"," +",engine='python')

df = df.convert_objects(convert_numeric=True)
sub_df1 = df.groupby(['RECL_LCC'])['COUNT'].sum()
sub_df2 = df.groupby(['RECL_PI'])['COUNT'].sum()
sub_df = concat([sub_df1,sub_df2],keys=["RECL_LCC","RECL_PI"]).unstack()
sub_df.plot(kind='bar',stacked=True,rot=1)
plt.show()

      



... gives: enter image description here

... which I think is what is required.

+5


source







All Articles