【问题标题】:Retrieve data based on date from excel with pandas使用 pandas 从 excel 中根据日期检索数据
【发布时间】: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


【解决方案1】:

我根据您的原始帖子提供了一个示例数据:

         date home_team away_team  home_score  away_score tournament     city   country neutral
0  2011-11-30  Scotland   England           0           0       FIFA  Glasgow  Scotland   FALSE
1  2012-03-08   England  Scotland           4           2       FIFA   London   England   FALSE
2  2010-03-07  Scotland   England           2           1       FIFA  Glasgow  Scotland   FALSE
3  2009-03-06   England  Scotland           2           2   Friendly   London   England   FALSE
4  2010-04-03  Scotland   England           3           0       FIFA  Glasgow  Scotland   FALSE
5  2009-03-25  Scotland     Wales           4           0   Friendly  Glasgow  Scotland   FALSE

你的代码基本没问题。我只是在开头和结尾更改了日期时间格式和打印格式。


df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d') # format shoud be matched with your data

# 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("The country that has scored the most goals in FIFA events is: ")
print(sub_df2['country'].iloc[0], sub_df2['total_score'].iloc[0])

此代码将打印:

The country that has scored the most goals in FIFA events is: 
England 6

【讨论】:

  • 感谢您根据您的建议更新了代码。但是,我得到的返回值是“空数据框”有什么想法吗?
  • 在你最近编辑的代码中,好像没有数据,哪个年份值大于2010,对吧? @user3042226
  • 是的,正确我想查找大于 2010 年的年份值。我的 excel 上有数据。有任何想法吗? @Sangkeun 公园
  • @user3042226 那么,如果您更新当前数据以便人们可以尝试,将会很有帮助。
  • 好的,我已经把新数据贴在上面了,谢谢
猜你喜欢
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 2016-02-27
相关资源
最近更新 更多