0

I have writting a .csv dataset from some data being picked from the internet but within this new dataset I have a column with date of some clients were contacted and I would like to while I am writing this new dataset in .csv, I instruct python to sort ascending all cases according to date column, so that when I do Scatter Plot automatically the line chart follow the number of cases per day in ascending order of date. Please, note the missing values, they must come as they are with empty fields so that afterwards I can deal with them filling or removing. Thanks. My code is as follow:

with open('demo.csv', "w") as myfile:
    print(mydata.decode('utf-8'),file=myfile)

date    enrolled
6/29/2018   1
6/29/2018   1
6/29/2018   
6/29/2018   1
6/20/2018   1
6/22/2018   1
6/19/2018   1
6/27/2018   1
6/28/2018   
6/27/2018   1
6/19/2018   1
6/20/2018   1
6/27/2018   1
6/27/2018   
6/26/2018   1
6/27/2018   
6/27/2018   1
Miguel Bambo
  • 15
  • 1
  • 6
  • 1
    Can you post sample data with expected output? – Rakesh Jun 30 '18 at 16:16
  • @Rakesh, I have posted.Thanks – Miguel Bambo Jun 30 '18 at 16:28
  • Possible duplicate of [Sorting a csv object by dates in python](https://stackoverflow.com/questions/9917865/sorting-a-csv-object-by-dates-in-python) – Georgy Jun 30 '18 at 16:48
  • [Python - How to sort csv data by date in the dd-mmm-yy format?](https://stackoverflow.com/questions/30040371/python-how-to-sort-csv-data-by-date-in-the-dd-mmm-yy-format) – Georgy Jun 30 '18 at 16:50
  • @Georgy, I can be wrong but I cant understand you when you say possible duplicate: by this you mean the solution of my problem is in that post? Please, explain me. My csv is being written first from an API (original source is Json), then what I need is how to instruct python to sort date column while I am writting this new demo.csv file and not while I am opening it. If it is not still clear, please, advise. Thanks – Miguel Bambo Jun 30 '18 at 17:09
  • @MiguelBambo "Possible duplicate" is an automatic message when someone flags a question as something that was already asked before. And your question (disregarding irrelevant parts) was asked here several times. – Georgy Jun 30 '18 at 19:06

1 Answers1

0

Use datetime.strptime to turn your date strings into datetime object so that they can be comparable as a key to sort your data list with.

from datetime import datetime
data = [line.split() for line in mydata.decode('utf-8').split('\n')]
header = data.pop(0)
data.sort(key=lambda c: datetime.strptime(c[0], '%m/%d/%Y'))
data.insert(0, header)
with open('demo.csv', "w") as myfile:
    for row in data:
        print('   '.join(row), file=myfile)

This outputs:

date   enrolled
6/19/2018   1
6/19/2018   1
6/20/2018   1
6/20/2018   1
6/22/2018   1
6/26/2018   1
6/27/2018   1
6/27/2018   1
6/27/2018   1
6/27/2018
6/27/2018
6/27/2018   1
6/28/2018
6/29/2018   1
6/29/2018   1
6/29/2018
6/29/2018   1
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Thanks for quick response. Please, as I am getting the dataset from an API through mydata vector and writting it to demo.csv how will I copy your code and complete may code as follow: with open('demo.csv', "w") as myfile: print(mydata.decode('utf-8'),file=myfile) and get same results as yours? – Miguel Bambo Jun 30 '18 at 16:54
  • Just insert my code (remove the whole `mydata = ....` part first) before you write `mydata` to a file, so before the `with open('demo.csv', "w") ` line. – blhsing Jun 30 '18 at 16:59
  • Never mind. I just edited my answer so that you can use it directly without any modifications. – blhsing Jun 30 '18 at 17:09
  • thanks for your efforts but maybe this is not working because I am using your python code inside my DASH Framework code. I am designing my Dashboard using Dash from Plot.ly on my localhost. – Miguel Bambo Jun 30 '18 at 17:50
  • 1
    Regardless of the framework you're using, if your original code above worked (without sorting), my code would work too. If your original code doesn't work, then you have to fix your code first before worrying about sorting. My code doesn't do anything other than reading the same `mydata` variable and writing a new value to the same `demo.csv` file your were writing to. – blhsing Jun 30 '18 at 17:57