【发布时间】:2023-04-01 04:05:02
【问题描述】:
大家好,我被困在使用 Python 和 Pandas 的以下步骤中。我正在尝试检索自 2010 年以来在“FIFA”比赛中进球最多的国家/地区。
[excel表格图片][1]
下面是一段代码,不知道行不行,我还在学习中。
#Which country has scored the most goals in FIFA events (qualifiers,
#cups, etc.) since 2010?
# convert to datetime format
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
# to get the most goals by sum
df['total_score'] = df['home_score'] + df['away_score']
#Not sure how to check all data as long as theres the word FIFA in it?
sub_df = df[ (df['tournament'] == "FIFA") & (df['neutral'] != "True") ]
#Not sure how to check all data as long as theres the word FIFA in it?
sub_df = df[(df['tournament'] == "FIFA") & (df['date'].dt.year >= 2010)]
# to get maximum total_score
sub_df2 = sub_df[sub_df['total_score'] == sub_df['total_score'].max()]
print(sub_df2)
**Empty DataFrame
Columns: [date, home_team, away_team, home_score, away_score,
tournament, city, country, neutral, total_score]
Index: []**
编辑:更新代码但不确定如何返回目标最多的国家
df['date'] = pd.to_datetime(df['date'], format='%Y/%M/%D')
# to get the most goals by sum
df['total_score'] = df['home_score'] + df['away_score']
#to get data only from year 2010 and up and events that have the word
#FIFA in it
sub_df = df[(df['date'].dt.year >= 2010) &
(df['tournament'].str.contains('FIFA'))]
#Which country has scored the most goals in FIFA events (qualifiers,
#cups, etc.) since 2010?
sub_df2 = sub_df[sub_df['total_score'] == sub_df['total_score'].max()]
#print the return value of the country with most goals
print(sub_df2)
print(sub_df2['country'], sub_df2['total_score'].max())
print("The country that has scored the most goals in FIFA events is: ")`
这是返回的内容: [在此处输入图片说明][1]
我想回到在所有 FIFA 赛事中进球最多的国家 [1]:https://i.stack.imgur.com/DxONa.png
【问题讨论】:
-
sub_df = df[(df['tournament'] == "Friendly") & (df['neutral'] != 'TRUE')]? -
您为 'sub_df' 分配了两次值,因此对 'sub_df' 的第一次分配被第二次分配覆盖。这是你想要的吗?
标签: python python-3.x pandas dataframe