【发布时间】:2022-01-21 01:33:26
【问题描述】:
我想从这个函数返回一个数据框,它可以在其他地方使用(准确的绘图)。
我的想法是使用我可以通过points_sum() 创建的数据框,将其保存为团队名称,然后在我的px.line(dataframe = team_name) 中使用该数据框。
本质上,我想在创建 men_points_df 变量后使用它。
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
在 plotly 中,我正在尝试使用我的新数据框 (men_points_df),如下所示,但我收到错误 undefined name,即使我可以打印它(例如:test = points_sum("FIF") (FIF 是团队名称之一)并在控制台中显示正确的数据框(当我输入 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
【问题讨论】:
-
错误是什么?
-
我顺便看过这篇文章:stackoverflow.com/questions/45579525/…,但我不知道是什么:将 create_df() 的结果分配给 df,像这样 df = create_df(),我应该在哪里这样做,所以我会工作
-
那么您是否只是在创建
men_points_df变量后尝试使用它? -
当我尝试使用数据框时,错误实际上发生了。当我说: test = points_sum("FIF") (其中一个团队)时,我得到了一个正确的数据框,但是当我尝试在情节中使用它时,它说“未定义”,即使我可以打印正确的结果控制台
-
完全正确。顺便说一句,使用蜘蛛 IDE,如果感兴趣的话