0

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
1
  • pandas has a really nice documentation. See here some examples on visualization. Commented Dec 14, 2015 at 15:55

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Is there anyway to create sub data frame for each A, B and C
0

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

2 Comments

maybe plt.plot(df.ix[df.Name == 'A', ['Date', 'Activity']])?
eeeh you're right! When I answered the df code window in OP's question was plain text so I didn't notice and couldn't edit it either bc there were pending edits... corrected!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.