Workspace

Visualization for next marketing campaign

0
Beta
Spinner

Visualization for next marketing campaign

📖 Background

You are a data analyst at a crowdfunding site. For the next quarter, your company will be running a marketing campaign. The marketing manager wants to target those segments that have donated the most in the past year. She turned to you to help her with her upcoming meeting with the CEO.

💾 The data

You have access to the following information:

Historic crowdfunding donations
  • "category" - "Sports", "Fashion", "Technology", etc.
  • "device" - the type of device used.
  • "gender" - gender of the user.
  • "age range" - one of five age brackets.
  • "amount" - how much the user donated in Euros.
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
df = pd.read_csv('./data/crowdfunding.csv')
df.head()

💪 Challenge

Create a single visualization that the marketing manager can use to explore the data. Include:

  1. What are the top three categories in terms of total donations?
  2. What device type has historically provided the most contributions?
  3. What age bracket should the campaign target?

✅ Checklist before publishing

  • Rename your workspace to make it descriptive of your work. N.B. you should leave the notebook name as notebook.ipynb.
  • Remove redundant cells like the judging criteria, so the workbook is focused on your answers.
  • Check that all the cells run without error.

⌛️ Time is ticking. Good luck!

df.dtypes
df.isnull().sum()
from plotly.subplots import make_subplots
import plotly.graph_objects as go
from plotly.offline import plot


fig = make_subplots(rows=4, cols=3, specs=[[{"type": "bar"},{"type":"table"},{'type':'pie'}], 
                                           [{"colspan": 2},None,{"type":"table"}],
                                           [{"type": "bar"},{"type":"bar"},{'type':'pie'}],
                                           [{"type": "table"},{"type":"table"},{'type':'table'}]],
                    subplot_titles=("Amount Contributed for each device type", "Amount contributed for each category by iOS Devices",
                                    "Amount Contributed by each category by Android Devices","Amount Contributed by each Category in Percentages",
                                    "Table Showing Amount Contributed by each Category","Amount contributed by Each Age Group with iOS Device",
                                    "Amount contributed by Each Age Group with Android Device","Total Amount Conrtibuted by Age Groups",
                                    "Total Amount Conrtibuted by Age Groups with iOS Devices","Total Amount Conrtibuted by Age Groups with Android Devices",
                                    "Total Amount Contributed By age Groups"))

a=df.groupby('device').sum().reset_index()
fig.add_trace(go.Bar(
     y=a['amount'],
     x=a['device'],
     name="Device"),
     row=1, col=1)

a=df.groupby(['device','category']).sum()
a=pd.DataFrame(a)
a.reset_index(inplace=True)
android=a[a["device"]=="android"]
ios=a[a["device"]=="iOS"]


fig.add_trace(go.Pie(
     values=ios['amount'],
     labels=ios['category'],
     name="iOS"),
     row=1, col=2)

fig.add_trace(go.Pie(
     values=android['amount'],
     labels=android['category'],
     name="Android"),
     row=1, col=3)



a=df.groupby('category').sum().reset_index()
a['percentage'] = round((a['amount']/a.amount.sum())*100,2)


fig.add_trace(go.Scatter(
     y=a['percentage'],
     x=a['category'],
     name="Category"),
    row=2, col=1)

fig.add_trace(go.Table(header=dict(
            values=["Category","Amount","Amount(%)"],
            font=dict(size=15),
            align="left"
        ),cells=dict(
            values=[a[k].tolist() for k in a.columns],font=dict(size=12),
            align = "left",height=30)),row=2,col=3)

a=df.groupby(['age','category','device']).sum().reset_index()
iosdevices=a[a['device']=='iOS']

fig.add_trace(go.Table(header=dict(
            values=["Age","Category","Device","Amount"],
            font=dict(size=15),
            align="left"
        ),cells=dict(
            values=[iosdevices[k].tolist() for k in a.columns],font=dict(size=12),
            align = "left",height=30)),row=4,col=1)

androiddevices=a[a['device']=='android']
fig.add_trace(go.Table(header=dict(
            values=["Age","Category","Device","Amount"],
            font=dict(size=15),
            align="left"
        ),cells=dict(
            values=[androiddevices[k].tolist() for k in a.columns],font=dict(size=12),
            align = "left",height=30)),row=4,col=2)
bar1 = px.bar(iosdevices,x="age",y="amount",color='category')
bar2 = px.bar(androiddevices,x="age",y="amount",color='category')
a=df.groupby(['age']).sum().reset_index()


for trace in bar1.data:
    fig.add_trace(trace, 3, 1)
for trace in bar2.data:
    fig.add_trace(trace, 3, 2)
    
fig.add_trace(go.Pie(
     values=a['amount'],
     labels=a['age'],
     name="Age Group"),
     row=3, col=3)

a=df.groupby(['age']).sum().reset_index()
fig.add_trace(go.Table(header=dict(
            values=["Age","Amount"],
            font=dict(size=15),
            align="left"
        ),cells=dict(
            values=[a[k].tolist() for k in a.columns],font=dict(size=12),
            align = "left",height=30)),row=4,col=3)
    

fig.update_xaxes(title_text="Device Type",row=1,col=1)
fig.update_xaxes(title_text="Category", showgrid=False, row=2, col=1)
fig.update_xaxes(title_text="Age Group", row=3, col=1)
fig.update_xaxes(title_text="Age Group", row=3, col=2)

fig.update_yaxes(title_text="Amount Contributed",row=1,col=1)
fig.update_yaxes(title_text="Amount Contributed (%)", showgrid=False, row=2, col=1)
fig.update_yaxes(title_text="Amount Contributed", row=3, col=1)
fig.update_yaxes(title_text="Amount Contributed", row=3, col=2)

fig.update_layout(
    margin=dict(l=30, r=20, t=40, b=30),
    paper_bgcolor="LightSteelBlue",
    autosize=False,
    width=1350,
    height=1000,
    hovermode='closest',
    barmode='stack',
    title_text="Visualization for next marketing campaign",
    title_x=0.5,font=dict(size=12)
)
fig.update_annotations(font_size=12)
fig