如何绘制分类数据?

时间:2018-10-24 12:57:46

标签: python-2.7 pandas plot seaborn

基本数据包含有关machine用于特定operators的{​​{1}}的信息

activity

我有如下数据:

df.head()

问题是所有列都是machine_name activity Operator_name start_datetime end_datetime reasons_for_break duration Yash [HMC] PILLAR SUB ASSY MOUNTING ON BASE Abhishek 2018-10-10 00:50:20 2018-10-10 11:51:23 661 IMPERIAL SPINDLE MOTOR ASSEMBLY AND MOUNTING Abijith 2018-10-10 11:44:00 2018-10-10 12:26:42 42 V.R SPINDLE MOTOR ASSEMBLY AND MOUNTING Abijith 2018-10-10 11:21:02 2018-10-10 12:26:27 65 Gnutti Carlo-2[HMC] ATC MOUNTING ON MACHINE BASE Anantha Ramu 2018-10-10 00:54:59 2018-10-10 00:55:45 0 Gnutti Carlo-2[HMC] SPINDLE MOUNTING Anantha Ramu 2018-10-10 00:57:04 2018-10-10 00:58:55 MFD mistake 1 MMF-3[HMC] APC SUB ASSY MOUNTING ON BASE Ashok 2018-10-10 09:27:41 2018-10-10 12:04:31 APC UP DOWN 56 MMF-3[HMC] IT/DDRT MOUNTING ON BASE Ashok 2018-10-10 13:45:16 2018-10-10 15:13:30 88 Gnutti Carlo-2[HMC] PILLAR SUB ASSY MOUNTING ON BASE Balamurali 2018-10-10 09:17:04 2018-10-10 12:21:25 184 Gnutti Carlo-2[HMC] PILLAR SUB ASSY MOUNTING ON BASE Balamurali 2018-10-10 12:21:25 2018-10-10 13:18:54 Tea break 57 数据类型,期望categoricalstart_datetimeend_datetime格式,而datetimeduration数据类型

如何integer显示这些数据的所有信息?

我尝试了seaborn作为:

plotted

但出现错误:

import seaborn as sns sns.lmplot( x="Operator_name", y="duration", data=df, fit_reg=False, hue='machine_name', legend=True)

我该如何绘制和显示这些数据的信息?

尝试以下代码:

Categorical is not ordered for operation min

获得了x轴重叠的图

x axis overlapping

1 个答案:

答案 0 :(得分:0)

这是虚拟数据框

df = pd.DataFrame({'Operator_name':["Abhishek"]*4 + ['Abijith']*5 + ['Anamtha Ramu']*3,
                  'Duration': np.random.randint(10, 200, size = 12)})
df.head()

    Operator_name   Duration
0   Abhishek         153
1   Abhishek         188
2   Abhishek         51
3   Abhishek         189
4   Abijith          188

您可以使用groupby:

df1 = df.groupby('Operator_name').sum().reset_index()
df1

    Operator_name        Duration
0   Abhishek              299
1   Abijith               458
2   Anamtha Ramu          343

使用DataFrame.plot.bar功能

df1.plot.bar(x = 'Operator_name', y='Duration')

您也可以使用matplotlib:

import matplotlib.pyplot as plt
plt.bar(df['Operator_name'], df['Duration'])

对于Seaborn,请尝试以下代码:

sns.barplot(x = df['Operator_name'], y = df['Duration'], hue = df['machine_name'])