1

I have the following dataframe in pandas df_res, we can call it with a few rows as shown.

   date       time      weekday      team    recipient
 2019-08-30 14:49:22    Friday      team1   [email protected]
 2019-08-30 05:57:51    Friday      team1   notification@listcom
 2019-08-29 22:54:58    Thursday    team1   [email protected]
 2019-08-29 22:54:58    Thursday    team1   [email protected]
 2019-08-30 06:26:12    Friday      team1   [email protected]
 2019-09-05 14:16:22    Thursday    team1   [email protected]
 2019-09-05 14:16:22    Thursday    team1   [email protected]
 2019-09-04 22:54:59    Wednesday   team1   [email protected]
 2019-09-04 22:54:59    Wednesday   team1   [email protected]

I want to plot a chart where the x-axis is a date ranging from June 2019 to Oct 2019. The y-axis is the count of how many times a recipient got an email in a given date. So I would like to do a stacked bar chart where each stack will be a recipient. For ex: on the date '2019-08-30', there are 2 mails for '[email protected]' and 1 for 'notification@listcom'. So I will have two stacks for that day. I also want to be able to provide a list of a few recipients whose email counts I am interested in. I wrote the below code to do the same but I'm struggling with how to get a count of the number of recipients. Instead if the count is a list or column, I am able to plot but that is not useful for me as the dataframe is huge with over a thousand rows.

import plotly.graph_objects as go
import datetime


x = [datetime.datetime(year=2019, month=06, day=4),
 datetime.datetime(year=2019, month=11, day=5),
 datetime.datetime(year=2019, month=13, day=6)]
y = [2, 2, 5]

 fig = go.Figure(data=[go.Bar(x=x, y=y)])
 # Use datetime objects to set xaxis range
 fig.update_layout(xaxis_range=[datetime.datetime(2019, 06, 17),
                           datetime.datetime(2019, 10, 7)])
 fig.show()

UPDATED TO ADD CODE: 10/11/2019 # plotting the values

    trace = go.Bar(
           x=[datetime.datetime(year=2019, month=8, day=1),
              datetime.datetime(year=2019, month=10, day=1)],
           y=[res_test['recipients'].value_counts()],
           name='Plot the values '
          )

      data = [trace]
      layout = go.Layout(title="Emails per volunteer", xaxis= 
                 {'title':'Date'}, yaxis={'title':'Email count'},
               barmode='stack'
                )


       fig = go.Figure(data=data, layout=layout)
       py.iplot(fig)

Is this how I pass the value to the y-axis?

1 Answer 1

4

Data preparation

date =['2019-08-30 14:49:22',
       '2019-08-30 05:57:51',
       '2019-08-29 22:54:58',
       '2019-08-29 22:54:58',
       ]
rec = ['[email protected]','[email protected]','[email protected]','[email protected]']
data = pd.DataFrame({
         'date':date,
         'recipient':rec
         })

Sample dataframe:

data
               date             recipient date_extract
0 2019-08-30 14:49:22  [email protected]   2019-08-30
1 2019-08-30 05:57:51  [email protected]   2019-08-30
2 2019-08-29 22:54:58        [email protected]   2019-08-29
3 2019-08-29 22:54:58           [email protected]   2019-08-29

# Extract date from date_timestamp
data['date'] = pd.to_datetime(data['date'])
data = data.assign(date_extract = [str(i.date()) for i in data['date']])

Get email count: Total emails per day

new_data = data.groupby(by=['date_extract','recipient']).size()
print(new_data)

date_extract  recipient           
2019-08-29    [email protected]          1
              [email protected]             1
2019-08-30    [email protected]    2

As new_data is series you can directly pass it to x or y parameter in plotly.

Total Recipients

recipient_frequency = data['recipient'].value_counts()

# Recipient distribution
recipient_frequency
Out[]: 
[email protected]    2
[email protected]          1
[email protected]             1
Name: recipient, dtype: int64

# Total recipient count
len(recipient_frequency)
Out[]: 3

if you have an issue with plotly - plots, refer some tutorials here: https://github.com/SayaliSonawane/Plotly_Offline_Python

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much! Most helpful - all the steps and the link with tutorials. I don't see a bar chart tutorial in your github. Maybe you could make this use case and provide one so it would be helpful. I updated my code to plot the values but although I am able to see date range in x-axis and email count in y-axis, nothing is plotted.
I have now added bar chart tutorial. Hope that's helpful.
I am sorry if it wasn't helpful. github.com/SayaliSonawane/Plotly_Offline_Python/tree/master/… I have created a new tutorial. Hope that helps.
Thanks Sayali. I appreciate the effort. I have solved my problem last week in a different way, I'll update the post in a little bit. But I'm sure your tutorial will be helpful for folks who read this page in search of help with plotly.
@py_noob By now you've gathered enough rep to award other users with your up-votes. If you found Sayalis efforts helpful, an up-vote is highly appreciated by most users. As a new user, using your up-votes even earns you a badge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.