Home > Mobile >  return pandas dataframe from function
return pandas dataframe from function

Time:12-20

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

CodePudding user response:

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
  • Related