Home > other >  Dataframes and Pandas - Locating Occurrence
Dataframes and Pandas - Locating Occurrence

Time:07-16

So, I have a dataframe, where I got the following:

Match Team A Team B Winner
1 Brasil Germany Germany
2 Brasil Germany Brasil
3 France Denmark France
4 Denmark France Denmark

My question is, how would I find how many times team A won the match. Same for Team B.

Using isin by taking Team A and Searching on winner has not been giving me clear results. I am fairly new to Python and Pandas, so I am short on ideas.

CodePudding user response:

IIUC, is that what you're looking for?

df[df['Team A'] == df['Winner']].groupby('Team A').size().reset_index().rename(columns={0:'count'})

    Team A    count
0   Brasil    1
1   Denmark   1
2   France    1
df[df['Team B'] == df['Winner']].groupby('Team B').size().reset_index().rename(columns={0:'count'})
    Team B      count
0   Germany     1

CodePudding user response:

It's not very clear what you're looking for...

Team A Win Count:

>>> df[df['Team A'].eq(df['Winner'])].shape[0]
3

Team B Win Count:

>>> df[df['Team B'].eq(df['Winner'])].shape[0]
1
  • Related