0

I have two scores each day in a bar graph and I am denoting the difference between the score for each day. It works fine as is, but I want to exclude weekends. When I do this, I'm getting an error. I'm unsure what I am doing wrong, but any help is appreciated. I've also provided an image of the desired output - see: [1]: https://i.sstatic.net/7T5QU.png

import plotly.graph_objects as go
import math
import pandas as pd
import numpy as np

df1 = pd.DataFrame({
   'score': [11,42,31,22],
   'dates': ['7/1/2022','7/2/2022','7/3/2022','7/4/2022'],
   'weekend': [0,1,1,0],
})

df2 = pd.DataFrame({
   'score': [44,45,26,22],
   'dates': ['7/1/2022','7/2/2022','7/3/2022','7/4/2022'],
   'weekend': [0,1,1,0],
})

#df1 = df1[(df1['weekend'] == 0)] When I filter out weekends this doesn't work, otherwise it's fine
#df2 = df2[(df2['weekend'] == 0)]

y1 = df1['score']
y2 = df2['score']

c = []
i = 0

while i < len(y1):
    i = i
    q = y1[i]-y2[i] 
    c.append(str(q))
    i=i+1

fig = go.Figure(data=[
    go.Bar(name='SF Zoo', x=df1['dates'], y=y1),
    go.Bar(name='LA Zoo', x=df2['dates'], y=y2, text=c, textposition='outside')
])


fig.update_layout(barmode='group')
fig.show()


1 Answer 1

1

You have a few options to return the subset of values in y1 and y2 in an array-like format after filtering out weekends. Update your code with either of these options:

Option 1:

y1 = df1['score'].values
y2 = df2['score'].values

Option 2:

y1 = df1['score'].to_numpy()
y2 = df2['score'].to_numpy()

Option 3:

y1 = df1['score'].tolist()
y2 = df2['score'].tolist()
Sign up to request clarification or add additional context in comments.

1 Comment

Geez, I don't even know how didn't even think of this. I am so grateful, thank you!

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.