I have a csv file called "CleanReport.csv" with some sample data, shown here:
Name,Color,Age
Mark,Red,9999
Bob,Red,712
Alice,Green,1
Lisa,Pink,99
Jacob,Yellow,33
Corey,Orange,44
And using Python I'm trying to sort by the column 'Age' (at index 2) without loosing the header column's place at the beginning. Here is my code:
import csv
import operator
with open('CleanReport.csv', 'r') as sortrow:
reader = csv.reader(sortrow.readlines(), delimiter=',')
sortedlist = sorted(reader, key=operator.itemgetter(int(2)), reverse=True)
for row in sortedlist:
print(row)
I am very close. However, this is my result:
['Name', 'Color', 'Age']
['Mark', 'Red', '9999']
['Lisa', 'Pink', '99']
['Bob', 'Red', '712']
['Corey', 'Orange', '44']
['Jacob', 'Yellow', '333']
['Alice', 'Green', '1']
Where my desired result would be this:
['Name', 'Color', 'Age']
['Mark', 'Red', '9999']
['Bob', 'Red', '712']
['Jacob', 'Yellow', '333']
['Lisa', 'Pink', '99']
['Corey', 'Orange', '44']
['Alice', 'Green', '1']
As you can see it treats the contents of the third column as if they were a string, even after I cast it to int, and sorts them as if they were strings.
I also tried this code #sortedlist = sorted(reader, key=lambda x: int(x[2]), reverse=True)
, which sorts numerically as I want it to. However that only works if I remove the header row from the test data.
Is there an easier fix to this problem without having to
- take out the header row somehow and store it in a variable
- sort the data using
#sortedlist = sorted(reader, key=lambda x: int(x[2]), reverse=True)
- create a new csv with the header row appended to it
- append each row in sortedlist to the same new csv one by one using a loop?