0

I want to return a dataframe from this function, which can be used elsewhere (for plotly graph to be exact).

My idea is to use the dataframe I can create with points_sum(), save it as the team name, and then use that dataframe in my px.line(dataframe = team_name).

In essence, I want to use the men_points_df variable after I created it.

def points_sum(team):

    points = 0
    men_points = []

    for index, row in menscore_df.iterrows():
        if row['hometeam'] == team:
            if row['homegoals'] > row['awaygoals']:
                points += 2
            elif row['homegoals'] == row['awaygoals']:
                points += 1
            elif row['homegoals'] < row['awaygoals']:
                points == points

            date = str(row['date'])

            men_points.append([date, points])

        if row['awayteam'] == team:
            if row['homegoals'] < row['awaygoals']:
                points += 2
            elif row['homegoals'] == row['awaygoals']:
                points += 1
            elif row['homegoals'] > row['awaygoals']:
                points == points

            date = str(row['date'])
            men_points.append([date, points])

            men_points_df = pd.DataFrame(men_points, columns = ["Date", 'Points'])

    return men_points_df

In plotly, I am trying to use my new dataframe (men_points_df), like below, but I get the error undefined name, even though I can print it (for example: test = points_sum("FIF") (FIF is one of the team names) and it shows the correct dataframe in the console (when I type test):

elif pathname == "/page-3":
        return [html.H1('Seasonal performance',
                    style={'textAlign':'center'}),
                html.Div(
                    children=[
                    html.H2('Select team',style={'textAlign':'center'}),
                    html.Br(),
                    html.Br(),
                    dcc.Dropdown(
                        id='team_dd',
                        options=[{'label': v, 'value': k} for k,v in teams_all.items()],
                    )]),

                dcc.Graph(id="performance_graph")
        ]

        Output(component_id="performance_graph", component_property="figure"),
        Input(component_id="team_dd", component_property="value")

        def update_graph(option_selected):
                    title = "none selected"
                    if option_selected:
                        title = option_selected
                    line_fig = px.line(
                        test,         # <------------ THIS IS THE ISSUE
                        title = f"{title}",
                        x = "Date", y = "Points")

                    return line_fig
6
  • What's the error? Commented Dec 18, 2021 at 15:42
  • I have btw looked at this post: stackoverflow.com/questions/45579525/…, but i dont know what is ment by: assign the result of create_df() to df like this df = create_df(), and where i should do it, so i would work Commented Dec 18, 2021 at 15:45
  • So are you just trying to use the men_points_df variable after you create it? Commented Dec 18, 2021 at 15:46
  • The error actually occures later, when i try to use the dataframe. When i say: test = points_sum("FIF") (which is one of the teams), i get a correct dataframe, but when i try to use it in plotly, it says "not defined" even though i can print the correct results in console Commented Dec 18, 2021 at 15:47
  • exactly right. Using spider IDE btw, if that is of interest Commented Dec 18, 2021 at 15:48

1 Answer 1

1

Just call points_sum in the update_graph function, before you use test:

def update_graph(option_selected):  
    title = "none selected"
    if option_selected:
        title = option_selected
    
    # vvv Here vvv
    test = points_sum("FIF")
    line_fig = px.line(
        test,         #THIS IS THE ISSUE
        title = f"{title}",
        x = "Date", y = "Points")
            
    return line_fig
Sign up to request clarification or add additional context in comments.

7 Comments

It made it slightly better, but now i get the following instead: Undefined name "points_sum", on: test = points_sum("FIF")
Is points_sum defined in the same file?
It would be really helpful if you could upload the whole file somewhere so I could take a look. Like pastebin or GitHub gist.
No it's not. But i see where you are going. When I insert the points_sum into that file, I get a new Undefined name, for my menscore_df. So i suppose I should do them all in the same file? I though it was enough to have them loaded in my Variable Explorer
Its a report for university, so sadly i can't. But if I understand you correct -- functions, dataframes, lists etc. in same file, and then it should be ok ?
|

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.