Home > Back-end >  Sort date column in a python plotly table
Sort date column in a python plotly table

Time:10-15

the problem I face is, that the Date column is not ordered as I want: from the news date to the oldest date. And as you can see I used sort_values to sort it in the dataframe where it works.

Another question I have is, why are the timestamps shown in the plotly table, but not in the dataframe? How can I remove them from the plotly table?

Thanks in advance, cheers

data = response.json() 

data1 = data['data']
rates = data1['rates']
rates_dic = rates.items() 

df = pd.DataFrame(rates_dic) 
df[1] = [x[symbol] for x in df[1]]
df.rename(columns = {0:'Date', 1:'Price'}, inplace=True)
df.Price = 1 / df.Price 
df['Price'] = df['Price'].map('${:,.2f}'.format)
df['Date'] = pd.to_datetime(df['Date'])
df.sort_values(by='Date',ascending=False)

fig = go.Figure(data=[go.Table(
        columnorder = [1,2],
        columnwidth=[1,1],
        header=dict(values=list(df.columns),
                    fill_color='black',
                    align='center'),
        cells=dict(values=[df.Date, df.Price],
                    fill_color='black',
                    align='left'))
    ])
fig.layout.width=500
fig.layout.height=1000
fig.update_traces(header_font=dict(size=15))

Dataframe:

Date    Price
29  2022-10-13  $405.00
28  2022-10-12  $386.50
27  2022-10-11  $385.80
26  2022-10-10  $385.80
25  2022-10-09  $385.80
24  2022-10-08  $385.00
23  2022-10-07  $396.25
22  2022-10-06  $404.00
21  2022-10-05  $399.00
20  2022-10-04  $399.00
19  2022-10-03  $433.70
18  2022-10-02  $433.70
17  2022-10-01  $436.00
16  2022-09-30  $436.00
15  2022-09-29  $435.65
14  2022-09-28  $435.65
13  2022-09-27  $435.65
12  2022-09-26  $435.65
11  2022-09-25  $435.65
10  2022-09-24  $435.65
9   2022-09-23  $444.50
8   2022-09-22  $440.00
7   2022-09-21  $439.05
6   2022-09-20  $439.00
5   2022-09-19  $445.50
4   2022-09-18  $445.50
3   2022-09-17  $445.50
2   2022-09-16  $444.20
1   2022-09-15  $439.35
0   2022-09-14  $438.00

table plot

Documentation references: df.sort_values and Series.astype.

  • Related