Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14126

How to get spesific unique combinations of a dataframe using only dataframe operations?

$
0
0

So imagine I have this data that contain entries of 2 players playing a game one after another then after they both gain a win or lose they get a shared score(logic of this is unimportant numbers are random anyway this is just an example to describe what I want)

So there are scores obtained for every possible outcomes after player P1 and player P2 have played.

The logic of the game is not important all I want to know is If I can create a new dataframe of all unique combinations of these 4 players playing using my initial dataframe. So calculate a new score for all possible combinations of these 4 Players if they all play and get a score together, lets say their total scores would summed up.example:

Player_1 Player_2 Player_3 Player_4 Outcome_1 Outcome_2  Outcome_3 Outcome_4 ScoreP1       P2       P3        P4         win       win       win       win      72

and other possible unique combinations.

The key is to get a score of 30 from the combination where both P1 and P2 win and get 42 from the combination where both P3 and P4 have won and sum them to create the score if these 4 players have played and they have all won.

I can do this with generating unique combinations etc, but in a real use case with larger parameters etc. its too long and results in dirty hard to read code. What I want to know is is there a way to achieve this using only operations such as merge, groupby, join, agg etc.

import pandas as pddata = {"Player_1": ["P1", "P1", "P1", "P1", "P2", "P2", "P2", "P2", "P1", "P1", "P1", "P1", "P3", "P3", "P3", "P3"],"Player_2": ["P2", "P2", "P2", "P2", "P3", "P3", "P3", "P3", "P4", "P4", "P4", "P4", "P4", "P4", "P4", "P4"],"Outcome_1": ["win", "win", "lose", "lose", "win", "win", "lose", "lose", "win", "win", "lose", "lose", "win", "win", "lose", "lose"],"Outcome_2": ["win", "lose", "win", "lose", "win", "lose", "win", "lose", "win", "lose", "win", "lose", "win", "lose", "win", "lose"],"Score": [30, 45, 12, 78, 56, 21, 67, 90, 15, 32, 68, 88, 42, 74, 8, 93]}df = pd.DataFrame(data)print(df)
   Player_1 Player_2 Outcome_1 Outcome_2  Score0        P1       P2       win       win     301        P1       P2       win      lose     452        P1       P2      lose       win     123        P1       P2      lose      lose     784        P2       P3       win       win     565        P2       P3       win      lose     216        P2       P3      lose       win     677        P2       P3      lose      lose     908        P1       P4       win       win     159        P1       P4       win      lose     3210       P1       P4      lose       win     6811       P1       P4      lose      lose     8812       P3       P4       win       win     4213       P3       P4       win      lose     7414       P3       P4      lose       win      815       P3       P4      lose      lose     93

Viewing all articles
Browse latest Browse all 14126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>