1974sb
1974sb

Reputation: 69

Using pandas to plot data

I am new to stackoverflow. My data frame is similar to the one given below. I want to plot this data such that I can get a plot for A's activity, B's activity and C's activity separately.

How can I accomplish this using 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

Upvotes: 0

Views: 82

Answers (2)

Fabio Lamanna
Fabio Lamanna

Reputation: 21542

Another method would include pivot. Starting from your dataframe df, I would set the index to Date:

df = df.set_index('Date')

and then pivot the table according to your values:

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

This returns this structure:

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

And in base of your needs you can simply plot it with:

d.plot()

Actually you have some duplicate values in the example, but now the plot looks like the following. Hope that helps.

enter image description here

Upvotes: 2

mirosval
mirosval

Reputation: 6822

Use matplotlib. There you can use:

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()

Assuming df is your pandas DataFrame

Upvotes: 0

Related Questions