dspractician
dspractician

Reputation: 89

Dash Pandas Generate descriptive statistics Table

I have a dataset which is similar to below one. Please note that there are multiple values for a single ID.

import pandas as pd
import numpy as np
import random

df = pd.DataFrame({'DATE_TIME':pd.date_range('2022-11-01', '2022-11-05 23:00:00',freq='h'),
                   'Line1':[random.uniform(110, 160) for n in range(120)],
                   'Line2':[random.uniform(60, 100) for n in range(120)],
                   'ID':[random.randrange(1, 100) for n in range(120)],
                   'TIMEINTERVAL':[random.randrange(1, 200) for n in range(120)]})

df['VISIT'] = df['DATE_TIME'].dt.day

df['MODE'] = np.select([df['VISIT']==1, df['VISIT'].isin([2,3])], ['New', 'InProgress'], 'Done')

I use the following DASH code to make descriptive stats table.

df = df.describe()

app = Dash(__name__)

app.layout = dash_table.DataTable(
    data=df.to_dict('records'),
    columns=[{'id': c, 'name': c} for c in df.columns],
)

if __name__ == '__main__':
    app.run_server(debug=True)

But, I cannot see the column that comes when using describe method in pandas: e.g. std,mean, and so on. How should I twist my code so that I cann see those?

Upvotes: 0

Views: 181

Answers (1)

r-beginners
r-beginners

Reputation: 35240

It is displayed by resetting the index of the data frame.

import pandas as pd
import numpy as np
import random

df = pd.DataFrame({'DATE_TIME':pd.date_range('2022-11-01', '2022-11-05 23:00:00',freq='h'),
                   'Line1':[random.uniform(110, 160) for n in range(120)],
                   'Line2':[random.uniform(60, 100) for n in range(120)],
                   'ID':[random.randrange(1, 100) for n in range(120)],
                   'TIMEINTERVAL':[random.randrange(1, 200) for n in range(120)]})

df['VISIT'] = df['DATE_TIME'].dt.day

df['MODE'] = np.select([df['VISIT']==1, df['VISIT'].isin([2,3])], ['New', 'InProgress'], 'Done')

df = df.describe()

df.reset_index(inplace=True)

from dash import Dash, dash_table
from jupyter_dash import JupyterDash

#app = Dash(__name__)
app = JupyterDash(__name__)
    
app.layout = dash_table.DataTable(
    data=df.to_dict('records'),
    columns=[{'id': c, 'name': c} for c in df.columns],
)

if __name__ == '__main__':
    app.run_server(debug=True, mode='inline')

enter image description here

Upvotes: 2

Related Questions