如何在groupby之后绘制数据

时间:2016-09-14 15:25:10

标签: python pandas matplotlib

我有一个与此类似的数据框

import pandas as pd
df = pd.DataFrame([['1','3','1','2','3','1','2','2','1','1'], ['ONE','TWO','ONE','ONE','ONE','TWO','ONE','TWO','ONE','THREE']]).T
df.columns = [['age','data']]
print(df)   #printing dataframe.

我在其上执行了groupby函数以获得所需的输出。

df['COUNTER'] =1       #initially, set that counter to 1.
group_data = df.groupby(['age','data'])['COUNTER'].sum() #sum function
print(group_data)

现在我想用matplot lib绘制出来。请帮帮我..我无法想象如何开始和做什么。 我想使用计数器值和类似于条形图的东西进行绘图

2 个答案:

答案 0 :(得分:5)

尝试:

group_data = group_data.reset_index()

为了摆脱groupby()为您创建的多重索引。

您的print(group_data)会告诉您:

In [24]: group_data = df.groupby(['age','data'])['COUNTER'].sum() #sum function

In [25]: print(group_data)
age  data 
1    ONE      3
     THREE    1
     TWO      1
2    ONE      2
     TWO      1
3    ONE      1
     TWO      1
Name: COUNTER, dtype: int64

然而,重置会简化'新指数:

In [26]: group_data = group_data.reset_index()

In [27]: group_data
Out[27]: 
  age   data  COUNTER
0   1    ONE        3
1   1  THREE        1
2   1    TWO        1
3   2    ONE        2
4   2    TWO        1
5   3    ONE        1
6   3    TWO        1

然后,根据您想要绘制的内容,您可能需要查看Matplotlib docs

修改

我现在更仔细地阅读,你想创建一个' bar'图表。

如果是这种情况,那么我会退一步,在groupby结果上使用reset_index()。相反,试试这个:

In [46]: fig = group_data.plot.bar()

In [47]: fig.figure.show()

我希望这会有所帮助

答案 1 :(得分:1)

试试这个:

# This is a great tool to add plots to jupyter notebook
% matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt

# Params get plot bigger
plt.rcParams["axes.labelsize"] = 16
plt.rcParams["xtick.labelsize"] = 14
plt.rcParams["ytick.labelsize"] = 14
plt.rcParams["legend.fontsize"] = 12
plt.rcParams["figure.figsize"] = [15, 7]

df = pd.DataFrame([['1','3','1','2','3','1','2','2','1','1'], ['ONE','TWO','ONE','ONE','ONE','TWO','ONE','TWO','ONE','THREE']]).T
df.columns = [['age','data']]
df['COUNTER'] = 1

group_data = df.groupby(['age','data']).sum()[['COUNTER']].plot.bar(rot = 90) # If you want to rotate labels from x axis
_ = group_data.set(xlabel = 'xlabel', ylabel = 'ylabel'), group_data.legend(['Legend']) # you can add labels and legend