Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Export pandas to csv data but before doing so sort date by least to greatest

$
0
0

Upon looking and researching online it does seem that there a numerous way of doing this but not sure if it fits the way that I want it for. Personally I would still like my dates as this format "3/30/24". I am extracting data from some which has numerous amount of data and everything works as expected but when I tried to sort my date it just was sorting it lexicographically and this is only when I read from the csv rather than writing because I just want to to test if it could even sort in the first place after writing to it.

data_to_export = {"Company Name": ["Mcdonalds","Burgerking"],"Delivery Address":["123 lake rd", "124 west rd"],"Date": ["3/30/24", "1/23/24"],"Customer Name": ["Zack", "Peter"],}
    df = pd.DataFrame(data_to_export)    df.to_csv(join_move_to, index=False)

Above is some of the dummy data just so you can get a quick example than below is how I save it

I did have some sort of sort methods before this but it didn't work so I just deleted it but before writing to how can I let it know that I want to sort by date and keep in mind I still want my format as "(Month/Day/Year) "

I do understand this is a string so it must be converted to some sort of date then sorted but I can not find a way to do that

here is what I tried just so you guys can see

    df = pd.DataFrame(data_to_export)    df["Date"] = pd.to_datetime(df["Date"],format="%m/%d/%y")    df.sort_values(by="Date", inplace=True)    df.to_csv(path, index=False)

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>