Reputation: 73
I have input.csv file with below format
number day
5 Mon
4 Tue
3 Wed
2 Thu
and I want to copy this data to another output.csv file with reverse order
number day
2 Thu
3 Wed
4 Tue
5 Mon
I have written this code but don't know how to proceed with reverse thing
cols = ["number", "day"]
file_path = "input.csv"
pd.read_csv(file_path, usecols=cols).to_csv("output.csv", index=False)
Upvotes: 1
Views: 1080
Reputation: 499
number,day
5,Mon
4,Tue
3,Wed
2,Thu
with open('t.csv','r') as file:
f=csv.reader(file)
data=[]
for row in f:
data.append(row)
header=data[0]
rows=data[1:]
rows.sort(reverse=False)
with open('t_out.csv','w',newline='') as file_out:
f=csv.writer(file_out)
f.writerow(header)
f.writerows(rows)
number,day
2,Thu
3,Wed
4,Tue
5,Mon
Upvotes: 2