使用pandas绘制数据

时间:2015-12-14 15:41:17

标签: python pandas

我是stackoverflow的新手。我的数据框架类似于下面给出的数据框架。我想绘制这些数据,以便我可以分别得到A活动,B活动和C活动的情节。

如何使用pandas完成此操作?

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

2 个答案:

答案 0 :(得分:2)

另一种方法包括pivot。从数据框df开始,我将索引设置为Date

df = df.set_index('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

答案 1 :(得分: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

相关问题