I grouped the data by two columns ( 'a'
and 'b'
). And for each column, I cut it into bins. For each crossed bin, I would like to have the sum of 'c'
. Then I want to create a grouped plot for the sum of 'c'
. However, when I use .plot(kind='bar')
after the grouped data, the bars were not grouped together.
grouped = df.groupby([pd.cut(df['a'], [0, 1, 2, 3]), pd.cut(df['b'], [0, 101, 300, 500])])
grouped = grouped.c.sum()
grouped.plot(kind='bar')
I would like the graph to be grouped by 'a'
with different color standing for 'b'
. I have tried .unstack().plot(kind='bar')
; however, it returns an error:
cannot determine next label for type
Given the following example data
df = pd.DataFrame({'a': [8, 49, 81, 52, 22, 24, 32, 67, 24, 21, 78, 16, 86, 19, 4, 88, 4, 20, 20, 1],
'b': [2, 49, 49, 70, 31, 47, 98, 26, 55, 36, 17, 39, 56, 80, 52, 36, 42, 69, 73, 70],
'c': [22, 99, 31, 95, 16, 32, 81, 20, 58, 23, 53, 5, 0, 81, 8, 68, 16, 36, 35, 54]})
and your setup
grouped = df.groupby([pd.cut(df['a'], [16, 30, 67, 86]),
pd.cut(df['b'], [0, 26, 49, 100])])
grouped = grouped['c'].sum()
you have two options
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.