pandas 如何在groupby之后绘制数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39494246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to plot data after groupby
提问by Karan Kothari
I have a data frame similar to this
我有一个与此类似的数据框
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.
I performed the groupby function on it to get the required output.
我对其执行了 groupby 函数以获得所需的输出。
df['COUNTER'] =1 #initially, set that counter to 1.
group_data = df.groupby(['age','data'])['COUNTER'].sum() #sum function
print(group_data)
now i want to plot the out using matplot lib. Please help me with it.. I am not able to figure how to start and what to do. I want to plot using the counter value and something similar to bar graph
现在我想使用 matplot lib 绘制出来。请帮助我.. 我不知道如何开始和做什么。我想使用计数器值和类似于条形图的东西进行绘图
回答by Thanos
Try:
尝试:
group_data = group_data.reset_index()
in order to get rid of the multiple index that the groupby()
has created for you.
为了摆脱groupby()
为您创建的多个索引。
Your print(group_data)
will give you this:
你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
Whereas, reseting will 'simplify' the new index:
而重置将“简化”新索引:
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
Then depending on what it is exactly that you want to plot, you might want to take a look at the Matplotlib docs
然后根据您想要绘制的内容,您可能需要查看Matplotlib 文档
EDIT
编辑
I now read more carefully that you want to create a 'bar' chart.
我现在更仔细地阅读了您想要创建“条形”图表的信息。
If that is the case then I would take a step back and notuse reset_index()
on the groupby result. Instead, try this:
如果是这样的话,那么我会退后一步,并没有使用reset_index()
上的GROUPBY结果。相反,试试这个:
In [46]: fig = group_data.plot.bar()
In [47]: fig.figure.show()
I hope this helps
我希望这有帮助
回答by estebanpdl
Try with this:
试试这个:
# 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