验证panda中的条件数组

2024-09-28 21:10:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集,包含足球队和得分

我在寻找连续几次在比赛中进球少于1.5球的记录

我做到了:

import pandas as pd
df = pd.read_csv('https://www.football-data.co.uk/mmz4281/1920/F1.csv')
dh = df[['Date','HomeTeam','AwayTeam','FTHG','FTAG']]

# for all the match of one team
index_list = dh[(dh['HomeTeam'] == 'Paris SG') | (dh['AwayTeam'] == 'Paris SG')].index.tolist()
dh_psg = dh.iloc[index_list]
print(dh_psg)

以下是我正在寻找的一个例子:

这是有史以来打破纪录的连胜


          Date     HomeTeam      AwayTeam    FTHG  FTAG
5   7/08/2019       Montpellier  Rennes       0     1
11  10/08/2019      Nantes       Montpellier  0     0
14  12/08/2019      Montpellier  Paris SG     1     0
16  14/08/2019      Toulouse     Montpellier  1     0
18  18/08/2019      Montpellier  Strasbourg   0     0

print("the record-breaking streak of all time is", 5)

2-考虑到最后的日期,这是本书的破纪录续集


          Date     HomeTeam      AwayTeam     FTHG  FTAG
17  19/08/2020     Montpellier   Dijon        1      0
18  22/08/2020     Montpellier   Strasbourg   0      0

print("the record-breaking sequel to by taking into account the last date is", 2)

谢谢你的帮助


Tags: csvthedfdateindexsgpddh
1条回答
网友
1楼 · 发布于 2024-09-28 21:10:36

使用ltagg

msk=df[['FTHG','FTAG']].agg(sum,1).lt(1.5)
df=df[msk]
print(df.head())

输出:

          Date     HomeTeam    AwayTeam  FTHG  FTAG
5   10/08/2019  Montpellier      Rennes     0     1
11  17/08/2019       Nantes   Marseille     0     0
12  17/08/2019       Amiens       Lille     1     0
16  17/08/2019     Toulouse       Dijon     1     0
18  18/08/2019        Reims  Strasbourg     0     0

相关问题 更多 >