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
mbih
  • 5
  • 5
  • What's the error? –  Dec 18 '21 at 15:42
  • I have btw looked at this post: https://stackoverflow.com/questions/45579525/returning-a-dataframe-in-python-function/48368668, 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 – mbih Dec 18 '21 at 15:45
  • So are you just trying to use the `men_points_df` variable after you create it? –  Dec 18 '21 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 – mbih Dec 18 '21 at 15:47
  • exactly right. Using spider IDE btw, if that is of interest – mbih Dec 18 '21 at 15:48
  • Will you please add that part of your code to the question, @mbih? –  Dec 18 '21 at 15:51

1 Answers1

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
  • It made it slightly better, but now i get the following instead: Undefined name "points_sum", on: test = points_sum("FIF") – mbih Dec 18 '21 at 16:12
  • Is `points_sum` defined in the same file? –  Dec 18 '21 at 16:13
  • It would be really helpful if you could upload the whole file somewhere so I could take a look. Like pastebin or GitHub gist. –  Dec 18 '21 at 16:13
  • 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 – mbih Dec 18 '21 at 16:15
  • 1
    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 ? – mbih Dec 18 '21 at 16:19
  • Yes, that's right. –  Dec 18 '21 at 16:20
  • I will go forward with that, and then return to update the thread - hopefully it works. Thank you so much! – mbih Dec 18 '21 at 16:21