简体   繁体   English

在Python中跨多个列进行计数

[英]Counting across multiple columns in Python

I'm pretty new to Python and have searched the web for an answer to this but it is tricky to find without showing it as an example! 我是Python的新手,已经在网上搜索了有关此问题的答案,但是如果不将其作为示例,很难找到它!

The data I have data is here: Dataset 我有数据的数据在这里: 数据集

What I'm after is the number of times each 'HomeTeam' has appeared in both the 'HomeTeam' and 'AwayTeam' columns up to and including the date. 我要跟踪的是每个“ HomeTeam”在“ HomeTeam”和“ AwayTeam”列中直到该日期(包括该日期)出现的次数。 So for the last row of data in the sample, the input would be 'Fulham', and the output = 4. This is because 'Fulham' has appeared 4 times in the 'HomeTeam' and 'AwayTeam' columns. 因此,对于样本中的最后一行数据,输入将为“ Fulham”,输出为4。这是因为“ Fulham”在“ HomeTeam”和“ AwayTeam”列中出现了4次。 For the first row of data, again, the input would be 'Fulham', but the output = 1, as it is the first time 'Fulham' has appeared. 同样,对于第一行数据,输入将为“富勒姆”,但输出= 1,因为这是首次出现“富勒姆”。 For the sample dataset, the output should be: 对于样本数据集,输出应为:

[1,1,2,1,3,1,4]

My code so far only allows me to get the number of times each team has appeared in the 'HomeTeam' column only: 到目前为止,我的代码仅允许我获取每个团队仅出现在“ HomeTeam”列中的次数:

df['H Count'] =  df.groupby(['HomeTeam']).cumcount()+1

This gives me the output: 这给了我输出:

[1,1,1,1,2,1,2]

Any help would be much appreciated! 任何帮助将非常感激!

As I understand, the team currently in the HomeTeam is being used as input. 据我了解,目前正在使用HomeTeam中的团队作为输入。 I don't know how you read in the dataset, but I have just created lists below. 我不知道您如何读取数据集,但是我刚刚在下面创建了列表。 The logic should however be clear. 但是,逻辑应该清楚。 Having the below, I get [1, 1, 3] 有了以下内容,我得到了[1, 1, 3]

HomeTeam = list()
HomeTeam.append("Fulham")
HomeTeam.append("Tottenham")
HomeTeam.append("Fulham")
AwayTeam = list()
AwayTeam.append("Chelsea")
AwayTeam.append("Fulham")
AwayTeam.append("Liverpool")
H_Count = []

p = 1
''' The team in the HomeTeam is used as input'''
for team in HomeTeam:
    ''' Get the list up until the current row'''
    tmp_Home = HomeTeam[:p]
    tmp_Away = AwayTeam[:p]

    ''' Count the number of times team has occured in home and away'''
    H_Count.append(tmp_Home.count(team) + tmp_Away.count(team))

    p+=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM