加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何将两列pandas dataframe的元素绘制为直方图?

发布时间:2020-12-20 11:53:19 所属栏目:Python 来源:网络整理
导读:我有以下pandas数据帧: A B 1 3 0 2 1 4 0 1 0 3 我想绘制给定A的B实例的频率,如下所示: | | | __ B | | | | ___ | | | | | | | | | | | | |__|_|__|__|______________ A 因此,我尝试了以下内容: df2.groupby([df.A,df.B]).count().plot(kind="bar") 但是
我有以下pandas数据帧:

A                    B

    1                    3
    0                    2
    1                    4
    0                    1
    0                    3

我想绘制给定A的B实例的频率,如下所示:

|
     |
     |        __
 B   |       |  |
     |  ___  |  |
     |  | |  |  |
     |  | |  |  |
     |__|_|__|__|______________
                A

因此,我尝试了以下内容:

df2.groupby([df.A,df.B]).count().plot(kind="bar")

但是,我得到以下异常:

TypeError: Empty 'DataFrame': no numeric data to plot

因此,我的问题是如何根据A?的频率绘制B中元素的频率.

解决方法

这听起来像你想要的:
你可以使用 Series.value_counts()

print(df['B'].value_counts().plot(kind='bar'))

enter image description here

如果您不希望value_count已排序,则可以执行以下操作:

print(df['B'].value_counts(sort=False).plot(kind='bar'))

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读