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
6
  • 1
    Can you post sample data with expected output? Commented Jun 30, 2018 at 16:16
  • @Rakesh, I have posted.Thanks Commented Jun 30, 2018 at 16:28
  • Possible duplicate of Sorting a csv object by dates in python Commented Jun 30, 2018 at 16:48
  • Python - How to sort csv data by date in the dd-mmm-yy format? Commented Jun 30, 2018 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 Commented Jun 30, 2018 at 17:09

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
Never mind. I just edited my answer so that you can use it directly without any modifications.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.