Date format MM/DD/YY to YY/MM/DD

Hi,

I’m struggling with my table.
Since I try to sort it by date, it shows gives me wrong results.

confirmed = pd.read_csv('confirmed.csv')
confirmed = confirmed.drop(['Provincconfirmed = confirmed.groupby(confirmed['Country/Region']).aggregate('sum')e/State', 'Lat', 'Long'], axis=1)
confirmed = confirmed.T
confirmed = confirmed.sort_index(ascending=False)
countries = st.multiselect("Choose countries to analize :",confirmed.columns, default=['Poland', 'Germany', 'France', 'United Kingdom', 'Norway'])

I want it to sort dates by year, then month and day at the end.
But file originally has month, day, year format so streamlit cannot figure out it’s date but just numbers.

That’s what is shown

123123123

Link to data:
https://data.humdata.org/hxlproxy/api/data-preview.csv?url=https%3A%2F%2Fraw.githubusercontent.com%2FCSSEGISandData%2FCOVID-19%2Fmaster%2Fcsse_covid_19_data%2Fcsse_covid_19_time_series%2Ftime_series_covid19_confirmed_global.csv&filename=time_series_covid19_confirmed_global.csv

Debug info

  • Streamlit version: version 1.15.1
  • Python version: 3.10.8
  • Using: VS Code env
  • Libraries: pandas, streamlit, plotly, datetime
  • OS version: Windows 10
  • Browser version: Google Chrome

It looks like some of your code got jumbled in the copy-paste, so here’ what I think you are going for. Explanation in the comment lines.

import pandas as pd

confirmed = pd.read_csv('confirmed.csv')
confirmed = confirmed.drop(['Province/State', 'Lat', 'Long'], axis=1)
confirmed = confirmed.groupby(['Country/Region']).aggregate('sum')
confirmed = confirmed.T

####################
# move the index to a column so it's easier to work with
confirmed.reset_index(inplace=True) 
# convert to datetime data type, then grab just the date part (default year first)
confirmed['index'] = pd.to_datetime(confirmed['index']).dt.date 
confirmed.sort_values('index', ascending=False, inplace=True)
# optionally set date back as index (could convert back to strings if desired)
confirmed.set_index('index', inplace=True)
####################

default=['Poland', 'Germany', 'France', 'United Kingdom', 'Norway']
countries = st.multiselect("Choose countries to analize :",confirmed.columns, default=default)

st.dataframe(confirmed[countries])
2 Likes

Thank you so much.

It has been changed from this:

To this:

That’s what I was looking for