0

I have a script that creates many pandas data frames that look like this:

                          Name Position Team  Salary  Projection Opponent
4            Andy Isabella       WR  ARI    4500   14.440124      CAR
6             Kyler Murray       QB  ARI    7000   70.585769      CAR
7          DeAndre Hopkins       WR  ARI    8500   24.701569      CAR
9                   Ravens      DST  BAL    4000   25.095681      WAS
14            Mark Andrews       TE  BAL    6000   26.201007      WAS
32              Mike Davis       RB  CAR    5700   20.394783      ARI
34   Cordarrelle Patterson       WR  CHI    3700   20.315333      IND
79             Will Fuller       WR  HOU    5900   26.891008      MIN
110          Malcolm Brown       RB  LAR    4700   18.767278      NYG

Sometimes there will be 4 players of position "WR" and 2 "RB" and others there will be 3 players of position "WR" and 3 "RB". But there will always be exactly 9 players with 1 QB, 1 TE, and 1 DST

the function i have written to write this pandas dataframe to a csv file is below:

def write_csv():
    df2 = pd.DataFrame()
    for name, df in my_team.items():
        df2 = df2.append(df)
    df2.to_csv('test.csv', mode='a')

What I want to do is have it append the pandas dataframe to the csv file in a specific order based off column "Position". the order I want is based off of the person's "position" value and i want to go as follows:

  1. QB
  2. RB
  3. RB
  4. WR
  5. WR
  6. WR
  7. TE
  8. WR or RB (based off if there is an extra person of position RB or WR)
  9. DST

How would I do this?

gasbaitlad
  • 33
  • 6
  • That's pretty tricky - if I'm understanding the problem right, whether a WR sorts above or below TE depends on how many WR there are already. Is that correct? If not for that requirement, I'd suggest using [categorical sorting](https://stackoverflow.com/a/13839029/530160), or using [sort_values with the key argument](https://stackoverflow.com/a/54301218/530160). You may need to build a custom sort. – Nick ODell Oct 05 '20 at 05:08
  • Essentially 1 QB, 2 RBs and 3WRs will always sort above the TE, but after the TE, if there is an additional WR or RB in the data frame would determine which person would come after – gasbaitlad Oct 05 '20 at 05:33

0 Answers0