0

I have a csv file with four columns (no header). I would like to sort the file by the first, then second column, and store back to disk.

I can read the file in using pandas or numpy, no problem, but not sure how to sort it, and store.

Jacko
  • 12,665
  • 18
  • 75
  • 126
  • can't you sort it directly on excel or similar?! – leoschet Jun 18 '18 at 22:05
  • @leoschet yes, probably, but I want to know how to do this programmatically – Jacko Jun 18 '18 at 22:06
  • 2
    Possible duplicate of [How to sort a dataFrame in python pandas by two or more columns?](https://stackoverflow.com/questions/17141558/how-to-sort-a-dataframe-in-python-pandas-by-two-or-more-columns) – Evgeny Jun 18 '18 at 22:11
  • 1
    what've you tried? quick search gives me: [pandas doc](http://pandas.pydata.org/pandas-docs/version/0.19/generated/pandas.DataFrame.sort.html) and [stackoverflow](https://stackoverflow.com/questions/37787698/how-to-sort-pandas-dataframe-from-one-column) – leoschet Jun 18 '18 at 22:13

1 Answers1

5

just like you wanted to process:

  1. read / parse CSV into a DF
  2. sort DF
  3. export DF to CSV and write it to disk

If we chain all steps together, then we don't even need to create a variable for the DataFrame...


Demo:

(pd.read_csv('/path/to/file.csv', header=None)
   .sort_values([0,1])
   .to_csv('/path/to/result.csv', index=False, header=None))
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419