user11388671
user11388671

Reputation:

How to get the all the combinations of two columns? (Python)

[Update: I want the outcomes, not the fixtures. I've added code for my points table, which shows the current table, based on which I want to determine the probability of a team being in the top 4]

I'm trying to determine the possible combinations of outcomes for a Sports League's match fixture.

I have my data in the form of csv tables as printed below.

The Outcome column below would take the value of either the Home team or the Away team, (probability of 0.5)

I want to create a separate list with all the possible outcomes and then combine the results to see the probability of a team being in the top 4 given the points table posted as (df). I'm trying to see the probability given all the outcomes.

Just recently started coding on Python and having difficulty with this. Any help appreciated

import numpy as np
import matplotlib as plt
import seaborn_table as sb
import pandas as pd
from itertools import combinations

df = pd.read_csv('ipltable.csv')

df2 = pd.read_csv('ipl2.csv')
print(df2)

##X = df2[['Home']].assign(key=1).merge(df2[['Away']].assign(key=1))

#print(X)

#Y = pd.MultiIndex.from_product([df2.Home,df2.Away]).to_frame()

#print(Y)
print(df)
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
    Fixture  Home  Away  Outcome
0         1   RCB   CSK      NaN
1         2    RR    DC      NaN
2         3   CSK   SRH      NaN
3         4   RCB  KXIP      NaN
4         5   KKR    RR      NaN
5         6   CSK    MI      NaN
6         7    RR   SRH      NaN
7         8    DC   RCB      NaN
8         9   KKR    MI      NaN
9        10   SRH  KXIP      NaN
10       12   CSK    DC      NaN
11       13    MI   SRH      NaN
12       14  KXIP   KKR      NaN
13       15    DC    RR      NaN
14       16   RCB   SRH      NaN
15       17  KXIP   CSK      NaN
16       18    MI   KKR      NaN


  Team Name  Matches  Points    NRR  W  L
0       CSK        9      14  0.101  7  2
1        MI       10      12  0.357  6  4
2        DC       10      12  0.160  6  4
3       SRH        9      10  0.736  5  5
4      KXIP       10      10 -0.044  5  5
5       KKR       10       8 -0.013  4  6
6        RR        9       6 -0.474  3  6
7       RCB        9       4 -0.938  2  7

Upvotes: 0

Views: 772

Answers (1)

BENY
BENY

Reputation: 323396

If you need all product from two list of teams

Method 1 Assign new value then merge

df[['Home']].assign(key=1).merge(df[['Away']].assign(key=1))

Method 2 pd.MultiIndex

pd.MultiIndex.from_product([df.Home,df.Away]).to_frame()

Method 3 itertools.product

pd.DataFrame(list(itertools.product(df.Home.tolist(),df.Away.tolist())))

Upvotes: 2

Related Questions