I would like to create groups from my data frame. Teams with 1 in the corresponding row/column cannot stay in the same group.
How to create the largest groups and fond the minimum number of groups?
Idea
There are 5 teams (50 in the original dataframe) for some reason some teams have players in common. The data frame shows with 1 if two teams have a player in common if not, the cell is filled with nan. How many and which teams can play together at the same time?
Here a sample data frame
pd.DataFrame.from_dict(data={'team1': [np.nan,1.0,1.0,np.nan,np.nan],
'team2':[1.0,np.nan,np.nan,np.nan,np.nan],
'team3':[1.0,np.nan,np.nan,np.nan,1.0],
'team4':[np.nan,np.nan,np.nan,np.nan,np.nan],
'team5':[np.nan,np.nan,1.0,np.nan,np.nan]}, orient='index',
columns=['team1', 'team2', 'team3', 'team4', 'team5'])
team1 | team2 | team3 | team4 | team5 | |
---|---|---|---|---|---|
team1 | NaN | 1.0 | 1.0 | NaN | NaN |
team2 | 1.0 | NaN | NaN | NaN | NaN |
team3 | 1.0 | NaN | NaN | NaN | 1.0 |
team4 | NaN | NaN | NaN | NaN | NaN |
team5 | NaN | NaN | 1.0 | NaN | NaN |
Expected output
In this easy case the minimum number pf groups is 2, and the possible solution is:
group1 = ['team1', 'team4', 'team5']
group2 = ['team2', 'team3']
from Create set with exclusion rules from a dataframe
No comments:
Post a Comment