0

The original dataframe,df I had was:

 date        name   count
0  2017-08-07  ABC      12
1  2017-08-08  ABC      5
2  2017-08-08  TTT      6
3  2017-08-09  TAC      5
4  2017-08-09  ABC      10

Then the following code was used to transform to the new dataframe,df2 below:

df = pd.DataFrame({"date":["2017-08-07","2017-08-08","2017-08-08","2017-08-09","2017-08-09"],"name":["ABC","ABC","TTT","TAC","ABC"], "count":           ["12","5","6","5","10"]})
df = df.pivot(index='date', columns='name', values='count').reset_index().fillna(0)

Now dataframe,df2 is transformed to:

   date        ABC     TTT    TAC 
0  2017-08-07  12       0      0
1  2017-08-08  5        6      0
2  2017-08-09  10       0      5

Now I tried to plot the dataframe,df2 in order to show each day on the x -axis vs the values in the column names/day on y-axis:( ABC,TTT,TAC ) but I keep getting two straight lines.

Here is the code:

fig=pyplot.figure() 
ax=fig.add_subplot(1,1,1) 
ax.set_title('Plot') 
ax.plot(df2)
5
  • What do you want it to look like? Commented Nov 6, 2017 at 13:41
  • Thanks! Three different lines showing the of values the columns per day i.e for 2017-08-07 , a line for ABC which shows 12, and also for TTT and TAC representing 0 Commented Nov 6, 2017 at 13:49
  • Let me know if my answer isn't what you're looking for. Commented Nov 6, 2017 at 13:49
  • Thanks.It is what I am looking for. How do export it for presentation purpose? Commented Nov 6, 2017 at 14:01
  • See here for details: stackoverflow.com/questions/9622163/… If the answer helped, you could accept it. Thanks. Commented Nov 6, 2017 at 14:03

1 Answer 1

4

Set date as the index and call df.plot:

df

         date  ABC  TTT  TAC
0  2017-08-07   12    0    0
1  2017-08-08    5    6    0
2  2017-08-09   10    0    5

df.set_index('date').plot(subplots=True)
plt.show()

enter image description here


Or, in a single graph:

df.set_index('date').plot()
plt.show()

enter image description here

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

Comments

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.