MGBpy
June 25, 2018, 2:57pm
1
Hi all.
I have a .csv file which I read it to plot a scatter on my DASH app. My dataset looks like this:
and I want to do Scatter Plot (line chart) in DASH.
Dataset is a .csv like below:
date
enrolled
6/18/2018
1
6/18/2018
1
6/19/2018
1
6/20/2018
1
6/22/2018
1
6/20/2018
1
6/20/2018
1
6/20/2018
1
6/22/2018
1
6/18/2018
1
6/18/2018
1
6/18/2018
1
6/20/2018
1
6/22/2018
1
6/19/2018
1
6/21/2018
1
6/22/2018
1
6/22/2018
1
My inside code is:
dfb=pd.read_csv(‘date_vs_enrolled.csv’, encoding=“latin-1”)
trace1=go.Scatter(
x=dfb[‘date’],
y=dfb[dfb[“enrolled”] == 1].sum(),
name=‘Study Enrollment’
)
trace2=go.Scatter(
x=dfb[‘date’],
y=dfb[dfb[‘enrolled’] != 1].sum(),
name=‘Rejected Participation’,
)
Note: enrolled (1 is Yes, 2 is NO) but here we just have 1.
The result is so strange without a number of cases enrolled per day along the time.
What is my mistake in the above code?
Thanks.
Plotly dates have to either be:
1 - Datetime objects (you can modify your pd.read_csv
to automatically parse datetimes)
2 - Strings that are formatted as e.g. 2018-06-22
MGBpy
June 25, 2018, 3:05pm
3
Thanks Chris for your quick response, can I share my full code to let you modify or can you please, explain in ore details? I am new to DASH. I did not understand well your suggestion. Thanks
MGBpy
June 25, 2018, 3:34pm
4
I included parse_dates=True to be like this:
dfb=pd.read_csv(‘date_vs_enrolled.csv’, encoding=“latin-1”, parse_dates=True)
but did not solve it.
empet
June 25, 2018, 4:47pm
5
@MGBpy
In both traces define x
as follows:
x=pd.to_datetime(df['date'])
MGBpy
June 25, 2018, 4:52pm
6
I think that the problem is any thing related with the line plot. Look at attached file.
Thanks
MGBpy
June 25, 2018, 4:57pm
7
There is no line chart in the scatter plot.
MGBpy
June 25, 2018, 9:04pm
8
I found a way to almost fix my concern but it need some adjustment because the dates are not in order.
MGBpy
June 25, 2018, 9:07pm
9
y=dfb.groupby(‘date’)[‘enrolled’].value_counts(),
MGBpy
June 25, 2018, 9:17pm
10
y=dfb[‘date’].groupby(dfb.date).agg(‘count’),
MGBpy
June 26, 2018, 12:03am
11
y=dfb.groupby(‘date’).size(),
MGBpy
June 26, 2018, 12:47am
12
y=dfb.groupby(‘date’)[‘enrolled’].sum(),
MGBpy
July 1, 2018, 11:48am
13
I got to plot barchart only for total and not groupby with this code:
trace1=go.Bar(
x=pd.to_datetime(dfb[‘date’]),
y=dfb.set_index(‘date’).resample(‘M’)[“enrolled”].sum(),
)
How to do groupby date?
Thanks