python - Pandas 绘制一组

标签 python pandas

我想绘制以下数据,这是唯一文本的计数

Element              
Motor 1  thermiek        15
         tijd te lang     9
Motor 2  thermiek        12
         tijd te lang     3
Motor 3  thermiek         5
         tijd te lang     4
dtype: int64

by_element = data.groupby('Element')
by_element['Alarm tekst'].value_counts().plot(kind='bar')

代码结果

enter image description here

如何将情节分组为:

enter image description here

最佳答案

这应该可以得到一个类似于评论中链接的分组条形图:

gb = df.groupby(['Element','Alarm tekst'])
gb['Alarm tekst'].count().unstack().plot(kind = 'bar')

聚合栏的原始建议:

您应该包含 agg() 函数来计算总值。

data.groupby('Element').agg('count').plot(kind = 'bar')

如果您的第二列已按术语求和,您可以使用 agg(numpy.sum) 代替:

import numpy    
data.groupby('Element').agg(numpy.sum).plot(kind = 'bar')

关于python - Pandas 绘制一组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29450999/

相关文章:

python - 更改列名称

python - 在python中删除链表中的节点

python - Paramiko,sftp 无法从 linux 服务器下载到 windows

python - Pandas 数据框 : How to update multiple columns by applying a function?

python - Azure ML 实验运行失败并显示 'HttpLoggingPolicy' 没有属性 'DEFAULT_HEADERS_ALLOWLIST'

python - 在 Python 中组合两种不同的可迭代类型

python - 结合多个 Pandas read_csv 和/或 file.readline()

Python:满足条件的列中的求和值

python - 使用条件列绘制 pandas DataFrame

python - 有效地将不均匀的列表列表转换为用 nan 填充的最小包含数组