+2 votes
in Programming Languages by (12.4k points)
How can I sort a CSV file using Python code? The file should be sorted by one or more columns.

1 Answer

0 votes
by (22.2k points)
edited by

You can use Pandas module of Python to import your CSV file into a dataframe. You can sort the dataframe by any number of columns and then can export the sorted dataframe to a CSV file.

Here is an example. My CSV file 'test.csv' has the following data:

NameAgeDOB
AA291967
BB121867
CC451834
DD341945
EE382011
FF171745
GG221867
HH121756
II382000

You can try the following code to sort the CSV file. You can also use CSV module of Python instead of Pandas, but I prefer Pandas.

>>> import pandas as pd
>>> df = pd.read_csv('test.csv', sep=',') #Read CSV file and create a DataFrame
>>> df
  Name  Age   DOB
0   AA   29  1967
1   BB   12  1867
2   CC   45  1834
3   DD   34  1945
4   EE   38  2011
5   FF   17  1745
6   GG   22  1867
7   HH   12  1756
8   II   38  2000
>>> df1=df.sort_values(by=['Age'])  # sort by Age
  Name  Age   DOB
1   BB   12  1867
7   HH   12  1756
5   FF   17  1745
6   GG   22  1867
0   AA   29  1967
3   DD   34  1945
4   EE   38  2011
8   II   38  2000
2   CC   45  1834
>>> df1=df.sort_values(by=['Age','Name'])  # sort by Age and Name
  Name  Age   DOB
1   BB   12  1867
7   HH   12  1756
5   FF   17  1745
6   GG   22  1867
0   AA   29  1967
3   DD   34  1945
4   EE   38  2011
8   II   38  2000
2   CC   45  1834
>>> df1.to_csv('test.csv',index=False)  # write the sorted data to CSV

The output 'test.csv' now has the sorted data.

NameAgeDOB
BB121867
HH121756
FF171745
GG221867
AA291967
DD341945
EE382011
II382000
CC451834

...