import pandas as pd
import plotly.express as px
df = pd.read_csv('crowdfunding.csv')
df.head()
df.info()
# Selecting the number of results to plot
n = 3
# Group by category and sum of the amounts donated
cat_data = df.groupby('category', as_index=False)[['amount']].sum().nlargest(n, 'amount')
# Create a list of top three categories for ease of use
cat_list = cat_data['category'].values.tolist()
# Create new dataframe with only the top three categories; sorted by age for the visualization
data = df[df['category'].isin(cat_list)].sort_values('age')
# Visualization
fig = px.histogram(data,
x='age',
y='amount',
color='device',
barmode='group',
facet_col='category',
title='Top {} categories by donation amount and device'.format(n))
fig.show()
for i in cat_list:
j = df[df['category'] == i]
k = j.amount.sum()
print(i, ': ', '€', k)
device_totals = df.groupby('device')[['amount']].sum()
device_totals
total_sum = df.amount.sum()
young_sum = df[df['age'] == '18-24'].amount.sum()
x = young_sum / total_sum
print('total sum = €{} (thousand)'.format(total_sum / 1000))
print('percent of total donation: {}%'.format(x.round(3) * 100))