How to make a Sorted by a specific csv column?

Asked

Viewed 61 times

1

I have a file. csv and need to make a Sorted by a specific column ('salary' column) and list by the column 'name' the top 10 salaries without using PANDAS. Can you help me? Thanks in advance.

   lista_top_ten = {} 
    reader = csv.DictReader(arquivo.csv)

    for linha in reader:
        for chave, valor in linha.items():
            try:
                lista_top_ten[chave].append(valor)
            except KeyError:
                lista_top_ten[chave] = [valor]


    sort_top_ten = sorted( reader, reverse=True, key=operator.itemgetter('salario'))

1 answer

1

I made an example with json but it’s the same idea with csv files. You can do something like this:

def ordenarSalario(value):
  return value['salario']

def os10MaioresSalarios():
    result = []
    data = json.loads(json_api)
    data.sort(reverse=True, key=ordenarSalario)
    for pessoa in data[:10]: 
      result.append(pessoa["nome"])
    return result

Browser other questions tagged

You are not signed in. Login or sign up in order to post.