0

I have a Pandas Data frame that shows the top games played by players in descending order:

Out[7]: 
    customer_id     game  total_plays
0            15  Numbers            2
1            15  Tripler            2
2            15      Max            2
3            15  Lottery            2
4             2     KENO            1
5             2      Max            1
6            44     Miss            4
7            46    Words           13
8            16  Lottery           15
9            17  Numbers           59
10            4      Max           10
11            5  Numbers           18
12            5      Max           14
13           23    Words            4
14           28  Instant           17
15           66      Max            2
16           87      Max            7
17           90     Spin           10
18          122      Max            9
19           45      Max          122
20           45  Numbers           23
21           45     KENO           15
22           19      Max           11
23           61  Instant           19
24           71  Numbers            1
25           53      Max            1
26          223      Max            2
27          105  Numbers            4
28           99  Numbers           35
29           18      Max            6

So the first game listed for each player is their most played game.

In my Data frame, a player could have played only 1 game, to a maximum of 4 games.

What I am trying to do is to create a new Data frame that has 5 columns:

  1. Player ID
  2. Top Game Played
  3. 2nd most played game (if any)
  4. 3rd most played game (if any)
  5. 4th most played game (if any)

Within the 1st to 4th most played games column, the name of the game must be named. If the player doesn't have a 2nd/3rd/4th most played game, the result can be left as 0.

So the table, based on the snippet of my data above should look something like this:

Out[6]: 
    customer_id top_game 2nd_game 3rd_game 4th_game
0            15  Numbers  Tripler      Max  Lottery
1             2     KENO      Max        0        0
2            44     Miss        0        0        0
3            46    Words        0        0        0
4            16  Lottery        0        0        0
5            17  Numbers        0        0        0
6             4      Max        0        0        0
7             5  Numbers      Max        0        0
8            23    Words        0        0        0
9            28  Instant        0        0        0
10           66      Max        0        0        0
11           87      Max        0        0        0
12           90     Spin        0        0        0
13          122      Max        0        0        0
14           45      Max  Numbers     KENO        0
15           19      Max        0        0        0
16           61  Instant        0        0        0
17           71  Numbers        0        0        0
18           53      Max        0        0        0
19          223      Max        0        0        0
20          105  Numbers        0        0        0
21           99  Numbers        0        0        0
22           18      Max        0        0        0

How would I go along doing this?

Kevin
  • 1,659
  • 5
  • 16
  • 22

0 Answers0