How can the `.max` function in the dropdown list based on the sorted value?
Question:
I have a set of data with the date.
Data:
Date Minor Major Sales
01/01/2022 Clutlery Kitchen 1000
01/01/2022 Tops Clothes 100
08/01/2022 Shoes Clothes 200
02/03/2022 Clutlery Kitchen 2000
02/03/2022 Tops Clothes 200
02/03/2022 Shoes Clothes 400
12/05/2022 Rakes Garden 6
20/05/2022 Cooking Kitchen 8
25/05/2022 Clutlery Kitchen 200
01/06/2022 Tops Clothes 300
25/06/2022 Shoes Clothes 400
25/06/2022 Turf Garden 5
01/02/2022 Turf Garden 1
01/02/2022 Hoses Garden 2
02/04/2022 Turf Garden 2
02/04/2022 Hoses Garden 4
Using this method to get the month name from the month number.
Input:
month_labels = {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug',9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
df['month'] = pd.to_datetime(df['Date'], format = '%d/%m/%Y', errors='coerce').dt.month
x= df['month'].apply(lambda x: month_labels[x])
month_cat = list(x.unique())
sort_mm = sorted(month_cat, key=lambda m: datetime.strptime(m, "%b"))
Output:
print (sort_mm)
[‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’]
Data is able to sort when just print it but when put it into the dropdown’s value, the .max
is not functioning. (.max
is using to let the dropdown list auto display the latest month in the listing.)
dcc.Dropdown(id='month_dd', value= x.max(),
options = [{'label':x, 'value':x}
for x in sort_mm])
Output sequence will be:
Apr, Feb, Jan, Jun, Mar, May
How can the .max
function in the dropdown list based on the sorted value?
Answers:
Just get the max
of df.month
before mapping it to the month_labels
dictionary. I.e., set the value
to
value=month_labels[df['month'].max()]
I have a set of data with the date.
Data:
Date Minor Major Sales
01/01/2022 Clutlery Kitchen 1000
01/01/2022 Tops Clothes 100
08/01/2022 Shoes Clothes 200
02/03/2022 Clutlery Kitchen 2000
02/03/2022 Tops Clothes 200
02/03/2022 Shoes Clothes 400
12/05/2022 Rakes Garden 6
20/05/2022 Cooking Kitchen 8
25/05/2022 Clutlery Kitchen 200
01/06/2022 Tops Clothes 300
25/06/2022 Shoes Clothes 400
25/06/2022 Turf Garden 5
01/02/2022 Turf Garden 1
01/02/2022 Hoses Garden 2
02/04/2022 Turf Garden 2
02/04/2022 Hoses Garden 4
Using this method to get the month name from the month number.
Input:
month_labels = {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug',9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
df['month'] = pd.to_datetime(df['Date'], format = '%d/%m/%Y', errors='coerce').dt.month
x= df['month'].apply(lambda x: month_labels[x])
month_cat = list(x.unique())
sort_mm = sorted(month_cat, key=lambda m: datetime.strptime(m, "%b"))
Output:
print (sort_mm)
[‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’]
Data is able to sort when just print it but when put it into the dropdown’s value, the .max
is not functioning. (.max
is using to let the dropdown list auto display the latest month in the listing.)
dcc.Dropdown(id='month_dd', value= x.max(),
options = [{'label':x, 'value':x}
for x in sort_mm])
Output sequence will be:
Apr, Feb, Jan, Jun, Mar, May
How can the .max
function in the dropdown list based on the sorted value?
Just get the max
of df.month
before mapping it to the month_labels
dictionary. I.e., set the value
to
value=month_labels[df['month'].max()]