-2

Attempt # 2 at writing this correctly, here is what my dataset looks like and you can see my desired output below.

What would be the best way to achieve my desired output?

Data Set

    Name   Position                                               Scores
0      Jim      G                                                 [5, 6]
1     Bill     SG                                                    NaN
2      Tim     PG                                               [19, 22]
3      Bob     SF                                                 [2, 3]

Desired Output

     Name   Position                                               Scores
0      Jim      G                                                 5
1      Jim      G                                                 6
2     Bill     SG                                                 NaN
3      Tim     PG                                                 19
3      Tim     PG                                                 22
.....
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. Off-site links and images of text are not acceptable; we need your question to be self-contained, in keeping with the purpose of this site. – Prune Feb 17 '21 at 00:48
  • Please [include a minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of your MRE. – Prune Feb 17 '21 at 00:49

1 Answers1

0

You can simply use pd.explode on the 'Points' column.

    import pandas as pd
    
    data = {}
    data['Teams'] = ['SF', 'NY', 'SEA']
    data['Players'] = ['Jim', 'Joe', 'Bob']
    jim_points = [3,10, 15, 6, 30]
    joe_points = [10, 12, 15, 23, 3, 4, 5, 6,7]
    bob_points = [3,4,5,6,7,8,12,8,9,9,9,9,22,22,2222]
    data['Points'] = [jim_points, joe_points,bob_points ]
    
    df = pd.DataFrame(data)
    print(df)
    
    df = df.explode('Points')
    print(df)

## Input

  Teams Players                                             Points
0    SF     Jim                                 [3, 10, 15, 6, 30]
1    NY     Joe                    [10, 12, 15, 23, 3, 4, 5, 6, 7]
2   SEA     Bob  [3, 4, 5, 6, 7, 8, 12, 8, 9, 9, 9, 9, 22, 22, ...

## Output
  Teams Players Points
0    SF     Jim      3
0    SF     Jim     10
0    SF     Jim     15
0    SF     Jim      6
0    SF     Jim     30
1    NY     Joe     10
1    NY     Joe     12
1    NY     Joe     15
1    NY     Joe     23
1    NY     Joe      3
1    NY     Joe      4
1    NY     Joe      5
1    NY     Joe      6
1    NY     Joe      7
2   SEA     Bob      3
2   SEA     Bob      4
2   SEA     Bob      5
2   SEA     Bob      6
2   SEA     Bob      7
2   SEA     Bob      8
2   SEA     Bob     12
2   SEA     Bob      8
2   SEA     Bob      9
2   SEA     Bob      9
2   SEA     Bob      9
2   SEA     Bob      9
2   SEA     Bob     22
2   SEA     Bob     22
2   SEA     Bob   2222
norie
  • 9,609
  • 2
  • 11
  • 18