Lerner
Lerner

Reputation: 11

how to find all the combinations of team possible using python

This is the screenshot of the dataset. Using this dataset I need to find all the possible combinations of 8 players whose score will be less than 100. There are 2 teams players included in this dataset. So the new team should either contain 3:4 or 4:4 ratio of players from the old teams. And there must be at least one player from each position

dataset

Below is the code I have tried.

import pandas as pd
import numpy as np
import sklearn 
from sklearn.preprocessing import LabelEncoder
import random
import itertools
from itertools import product

"""Importing dataset"""

dataset = pd.read_excel("NBA_0.xlsx")
print (dataset)
df = pd.DataFrame(dataset) 
df[['Player', 'Postion']]

"""Label Encoding"""

label_encoder = LabelEncoder()
df['Postion'] = label_encoder.fit_transform(dataset['Postion'])
df['Team'] = label_encoder.fit_transform(dataset['Team'])
df[['Player', 'Postion', 'Team']]

"""Select one player from each position"""

df_PG_0 = df[(df['Postion'] == 0) & (df['Team'] == 0)]
df_PG_0.sample()

df_PG_1 = df[(df['Postion'] == 0) & (df['Team'] == 1)]
df_PG_1.sample()

df_SG_0 = df[(df['Postion'] == 1) & (df['Team'] == 0)]
df_SG_0.sample()

df_SG_1 = df[(df['Postion'] == 1) & (df['Team'] == 1)]
df_SG_1.sample()

df_PF_0 = df[(df['Postion'] == 2) & (df['Team'] == 0)]
df_PF_0.sample(2)

df_PF_1 = df[(df['Postion'] == 2) & (df['Team'] == 1)]
df_PF_1.sample()

df_C_0 = df[(df['Postion'] == 3) & (df['Team'] == 0)]
df_C_0.sample()

df_C_1 = df[(df['Postion'] == 3) & (df['Team'] == 1)]
df_C_1.sample()

df_SF_0 = df[(df['Postion'] == 4) & (df['Team'] == 0)]
df_SF_0.sample()

df_SF_1 = df[(df['Postion'] == 4) & (df['Team'] == 1)]
df_SF_1.sample()

df_sample_H = pd.concat([df_PG_0, df_SG_0, df_SF_0, df_PF_0, df_C_0]).sample(3)
print(df_sample_H)

df_sample_L = pd.concat([df_PG_1, df_SG_1, df_SF_1, df_PF_1, df_C_1]).sample(5)
print(df_sample_L)

df_sample_team = pd.concat([df_sample_L, df_sample_H]).sample(8)
print(df_sample_team)

"""select 3:4 players from each team"""

df_sample = pd.concat([df_PG_0,df_PG_1, df_SG_0, df_SG_1, df_SF_0, df_SF_1, df_PF_0, df_PF_1, df_C_0, df_C_1]).sample(8)
print(df_sample)


"""Credit Score should be less than 100"""

df_value = df_sample_team.Credits.cumsum() 
print(df_value)

Using the above code I could get only one team at a time. How can I modify my code such that I will get all the possible teams who score less than 100. There are 2 teams players included in this dataset. So the new team should either contain 3:4 or 4:4 ratio of players from the old teams. And there must be atleast one player from each position

Upvotes: 1

Views: 210

Answers (1)

Gonçalo Peres
Gonçalo Peres

Reputation: 13582

To get a df of all the teams who scored less than 100 Points, let's name it dfteams

dfteams = df.loc[df['Points'] < 100, 'Team']

One can also create a new column based on the condition and assign a value 1/0 if the condition is satisfied or not

cond = (df['Points']<100)
df['New'] = np.where(cond, 1, 0)

Upvotes: 1

Related Questions