2015-12-14 86 views
0

我是新来的stackoverflow。我的数据框与下面给出的相似。我想绘制这个数据,以便我可以分别获得A的活动情况,B的活动情况和C的活动情况。使用熊猫来绘制数据

我该如何使用熊猫来完成此操作?

Name Date   Activity 
A  01-02-2015  1 
A  01-03-2015  2 
A  01-04-2015  3 
A  01-04-2015  1 
B  01-02-2015  1 
B  01-02-2015  2 
B  01-03-2015  1 
B  01-04-2015  5 
C  01-31-2015  1 
+0

'pandas'有一个非常好的文档。在这里看到一些关于[可视化]的例子(http://pandas.pydata.org/pandas-docs/stable/visualization.html)。 – iled

回答

2

另一种方法将包括pivot。根据你的价值观

df = df.set_index('Date') 

,然后转动表:从您的数据框中df开始,我会索引设置为Date

d = pd.pivot_table(df,index=df.index, columns='Name', values='Activity').fillna(0) 

这将返回此结构:

Name  A B C 
Date     
2015-01-02 1 1.5 0 
2015-01-03 2 1.0 0 
2015-01-04 2 5.0 0 
2015-01-31 0 0.0 1 

并根据您的需求,您可以简单地将其绘制为:

d.plot() 

实际上,您在示例中有一些重复的值,但现在情节如下所示。希望有所帮助。

enter image description here

+0

有没有办法为每个A,B和C创建子数据帧 – 1974sb

0

使用matplotlib。在那里,你可以使用:

import matplotlib.pyplot as plt 
plt.figure() 
plt.plot(df.ix[df.Name == 'A', ['Date', 'Activity']]) 
plt.plot(df.ix[df.Name == 'B', ['Date', 'Activity']]) 
plt.plot(df.ix[df.Name == 'C', ['Date', 'Activity']]) 
plt.show() 

假设df是你的熊猫DataFrame

+0

也许'plt.plot(df.ix [df.Name =='A',['Date','Activity']])'? – lowtech

+0

eeeh你是对的!当我回答OP的问题中的df代码窗口是纯文本,所以我没有注意到,并且无法编辑它bc有待处理的编辑...更正! – mirosval