1

I'm trying to sort my CSV file so that the teams with the most gold, silver and bronze medals are at the top of the list and those with the least are at the bottom.

def rank_team(file_name):
import csv

file = open('medal.csv')
for line in file:
    print(line)

pass

rank_team('medal.csv')

This is the code that I have now. Just wondering how I would sort it all.

This is the CSV that I'm using. CSV used to sort

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • 1
    Do you want to sort by the most gold, then break ties by the most silver, then break ties by the most bronze? Or do you want to sort by the total number of medals? – Stef Jan 05 '22 at 10:19
  • Could you also please clarify whether you want to sort in ascending or descending order? Your title says ascending, but then you say you want the higher values at the beginning and lower values at the end, which is descending. – Stef Jan 05 '22 at 10:38
  • 1
    In fact what would be even better would be to include that csv file you screenshotted, as text in your post (rather than as an image), and then also include in the post, the output that you want (the sorted result). – Stef Jan 05 '22 at 10:39
  • Aww Okay, yeah I wanted to do the highest values at the beginning and then lower values at the end. Sorry for that. I'll quickly change the description. –  Jan 05 '22 at 10:40

4 Answers4

1

you can use sorted function with key condition.

if you want to sort ascending [Gold,Silver,Bronze], then this code will help you.

import csv


def rank_team(file_name):
    with open('medal.csv') as f:
        reader = csv.reader(f)
        header = next(reader)
        data = [row for row in reader]
    print(header)
    print(sorted(data, key=lambda x: (x[1], x[2], x[3])))


rank_team('medal.csv')
SEUNGFWANI
  • 140
  • 10
  • Okie, thank you! I had something similar to this before but it wasn't working. But then I realised I put some stuff wrong. Just overthinking things like usual. Thank you again! –  Jan 05 '22 at 10:43
1

Using the csv.reader and csv.writer functions, as well as sorted with a tuple key:

import csv

with open('medal.csv', 'r') as in_file:
    in_reader = csv.reader(in_file)
    header = next(in_reader)
    data = sorted(in_reader, key=lambda row: tuple(int(x) for x in row[1:]), reverse=True)

with open('sorted_medal.csv', 'w', newline='') as out_file:
    out_writer = csv.writer(out_file)
    out_writer.writerow(header)
    out_writer.writerows(data)

Result:

# Input: medal.csv
team,gold,silver,bronze
t1,17,12,38
t2,8,7,29
t3,17,11,39
t4,17,12,37
t5,8,9,30

# Output: sorted_medal.csv
team,gold,silver,bronze
t1,17,12,38
t4,17,12,37
t3,17,11,39
t5,8,9,30
t2,8,7,29
Stef
  • 13,242
  • 2
  • 17
  • 28
1

You can use pandas for this. Read the csv as a pd.DataFrame and use sort_values method:

import pandas as pd
df = pd.read_csv('medal.csv')
df = df.sort_values(by=['Gold','Silver','Bronze'], ascending=False)

Note: What you describe is descending order.

0

Here a link that might help you . Change the 3 by 0 since you want to use the first column.

Nabil
  • 1,130
  • 5
  • 11