0

I have the following DataFrame nq for which I want to plot a Line Plot.
The columns -1,-2 etc. indicate user score before and after the Date given in date column, i.e., if nq.date = 2020-10-23 then nq.-1 =2020-10-22. I tried the following code but it produces a KeyError: (-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7). Is there a way to fix this?

nq

UserId     |   date                 |-7|-6|-5|-4|-3|-2|-1|0 |1 |2 |3 |4 |5 |6 |7
     1      2009-10-17 17:38:32.590 |0 |0 |0 |0 |0 |0 |5 |0 |1 |0 |0 |0 |0 |0 |0  
     2      2009-10-19 00:37:23.067 |0 |0 |0 |0 |0 |2 |1 |0 |1 |0 |0 |8 |0 |0 |0    
     3      2009-10-20 08:37:14.143 |0 |0 |0 |0 |0 |0 |3 |0 |0 |0 |0 |0 |0 |0 |0 
     4      2009-10-21 18:07:51.247 |0 |7 |0 |0 |0 |0 |1 |0 |0 |0 |0 |0 |0 |0 |0 
     5      2009-10-22 21:25:24.483 |0 |0 |0 |0 |0 |0 |1 |0 |0 |0 |0 |0 |0 |0 |0  

code

nq.plot(kind='line',x=nq[-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7], y=nq[-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7].mean())
2
  • What should the graph look like? Commented Jul 18, 2020 at 15:32
  • @ipj the x axis should have labels from -7 to 7 and y axis should have the mean of the labels. Commented Jul 18, 2020 at 15:35

1 Answer 1

1

Try:

nq.iloc[:,2:].mean().plot(kind='line')

result:

enter image description here

To combine mean and std on chart try:

nq.iloc[:,2:].mean().plot(kind='line', label='mean')
nq.iloc[:,2:].std().plot(kind='line', label='std')
plt.legend()

enter image description here

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

3 Comments

How to add the standard deviation to the plot?
Is there a way to have the std as a shadow on the mean line?

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.