如何绘制groupby的value_counts
我每年都有一个具有多个观察结果的数据框。
df = pd.DataFrame({"year": [2000,2000,2000,2001,2001,2001], "A": [1,1,0,0,1,0], "B": [4,4,6,10,10,10]})
year A B
0 2000 1 4
1 2000 1 4
2 2000 0 6
3 2001 0 10
4 2001 1 10
5 2001 0 10
我按年进行对
df.groupby("year").A.value_counts()
year A
2000 1 2
0 1
2001 0 2
1 1
。
I have a dataframe with multiple observations in each year.
df = pd.DataFrame({"year": [2000,2000,2000,2001,2001,2001], "A": [1,1,0,0,1,0], "B": [4,4,6,10,10,10]})
year A B
0 2000 1 4
1 2000 1 4
2 2000 0 6
3 2001 0 10
4 2001 1 10
5 2001 0 10
I group it by year and examine value_counts for column A.
df.groupby("year").A.value_counts()
year A
2000 1 2
0 1
2001 0 2
1 1
How do I get a barchart showing the number of times A=1 in each year?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
unstack
indexa
进入列中,然后绘制:Try
unstack
indexA
into columns and then plot: