Python / Dash by Plotly:按日期对案例进行分组

时间:2018-07-01 11:54:06

标签: python plotly plotly-dash

我只能用以下代码绘制条形图,而不是按发生的日期对所有案件进行分组:

 trace1=go.Bar(
    x=pd.to_datetime(dfb['date']),
    y=dfb.set_index('date').resample('M')["enrolled"].sum(),
    )

如何按日期分组? 数据集如下

date    enrolled
6/29/2018   1
6/29/2018   1
6/29/2018   
6/29/2018   1
6/20/2018   1
6/22/2018   1
6/19/2018   1
6/27/2018   1
6/28/2018   
6/27/2018   1
6/19/2018   1
6/20/2018   1
6/27/2018   1
6/27/2018   
6/26/2018   1
6/27/2018   
6/27/2018   1

谢谢

3 个答案:

答案 0 :(得分:0)

我在data.csv中使用了以下内容:

date   enrolled
6/29/2018   1
6/29/2018   1
6/29/2018   0
6/29/2018   1
6/20/2018   1
6/22/2018   1
6/19/2018   1
6/27/2018   1
6/28/2018   0
6/27/2018   1
6/19/2018   1
6/20/2018   1
6/27/2018   1
6/27/2018   0
6/26/2018   1
6/27/2018   0
6/27/2018   1

这是针对您的问题的一种可能解决方案:

import plotly
import plotly.graph_objs as go

import pandas as pd

# Read in data (might be already given in your code)
df = pd.read_csv('data.csv', delimiter='   ')
groups = df.groupby(['date'])

xData = []
yData = []

# Group data by keys and get sum
for group, data in df.groupby(['date']):
    xData.append(group)
    yData.append(data['enrolled'].sum())

data = [go.Bar(
            x=xData,
            y=yData
    )]

plotly.offline.plot(data, filename='a-simple-plot') 

此示例使用破折号发布图形:

import pandas as pd

# Read in data (might be already given in your code)
df = pd.read_csv('data.csv', delimiter='   ')
groups = df.groupby(['date'])

xData = []
yData = []

# Group data by keys and get sum
for group, data in df.groupby(['date']):
    xData.append(group)
    yData.append(data['enrolled'].sum())

# Dash specific stuff
import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()

app.layout = html.Div(children=[
    html.H1(
        children='Hello Dash',
    ),

    html.Div(children='Dash: A web application framework for Python.', style={
        'textAlign': 'center'
    }),

    dcc.Graph(
        id='example-graph-2',
        figure={
            'data': [
                {'x':xData, 'y': yData, 'type': 'bar'},
            ],
            'layout': {}
            }

    )
])

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

答案 1 :(得分:0)

`:a/b/fileA/fileB/fileC`:c/d/fileX/fileY

答案 2 :(得分:0)

nodes