pandas 数据框分组图 [英] Pandas dataframe groupby plot

查看:69
本文介绍了 pandas 数据框分组图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧,其结构为:

I have a dataframe which is structured as:

          Date   ticker  adj_close 
0   2016-11-21     AAPL    111.730     
1   2016-11-22     AAPL    111.800    
2   2016-11-23     AAPL    111.230    
3   2016-11-25     AAPL    111.790     
4   2016-11-28     AAPL    111.570    
...          
8   2016-11-21      ACN    119.680            
9   2016-11-22      ACN    119.480              
10  2016-11-23      ACN    119.820              
11  2016-11-25      ACN    120.740 
...             

如何根据代码adj_closeDate进行绘制?

How can I plot based on the ticker the adj_close versus Date?

推荐答案

简单图解,

您可以使用:

Simple plot,

you can use:

df.plot(x='Date',y='adj_close')

或者您可以预先将索引设置为Date,这样就可以轻松绘制所需的列:

Or you can set the index to be Date beforehand, then it's easy to plot the column you want:

df.set_index('Date', inplace=True)
df['adj_close'].plot()


如果要在图表上包含一个由ticker组成的系列

您需要先 groupby :


If you want a chart with one series by ticker on it

You need to groupby before:

df.set_index('Date', inplace=True)
df.groupby('ticker')['adj_close'].plot(legend=True)

grouped = df.groupby('ticker')

ncols=2
nrows = int(np.ceil(grouped.ngroups/ncols))

fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(12,4), sharey=True)

for (key, ax) in zip(grouped.groups.keys(), axes.flatten()):
    grouped.get_group(key).plot(ax=ax)

ax.legend()
plt.show()

这篇关于 pandas 数据框分组图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆