My goal is to produce a simple stacked bar plot with the VALUE as the X axis, each type is the bar stacks, and the number(count) is the y value of each bar stack.
I have the result of a data frame grouping which looks like:
VALUE | TYPE | |
---|---|---|
0.0 | 0 | 16 |
1 | 26 | |
2 | 15 | |
3 | 18 | |
1.0 | 1 | 15 |
2 | 2 | |
3 | 10 | |
2.0 | 0 | 10 |
1 | 175 | |
2 | 99 | |
3 | 11 | |
15 | 100 | |
3.0 | 0 | 92 |
1 | 20 | |
2 | 17 | |
3 | 26 | |
15 | 1 |
Name: VALUE, dtype: int64
I created this from a large data frame by: dataOutput = df.groupby(['VALUE','TYPE']).agg('VALUE').count()
I want to iterate through the dataframe and get access to the 'counts', and then push the uneven result into the stacked plots.
I am stuck at the looping through this data and getting access to the count. for i, (val, typ) in enumerate(df.index): print(val) print(typ)
How to access count?
for val, typ in df.groupby(level=0): print(val) print(typ) break
The data is there, but not sure how to access the count column.
VALUE | TYPE | |
---|---|---|
0.0 | 0 | 16 |
1 | 26 | |
2 | 15 | |
3 | 18 |
Is there some simpler way? Or can someone explain how I could condition this data.
IIUC, you have a multiindex series, s, that looks like this:
VALUE TYPE
0.0 0 16
1 26
2 15
3 18
1.0 1 15
2 2
3 10
2.0 0 10
1 175
2 99
3 11
15 100
3.0 0 92
1 20
2 17
3 26
15 1
Name: count, dtype: int64
For convenience, I renamed the series name to 'count'.
s.unstack().plot.bar(stacked=True)
Output: