Skip to content
Missing Value
import pandas as pd
import numpy as np
delivery_dataset = pd.read_csv("Delivery_dataset.csv")
print(delivery_dataset.head())
#1. Find the total number of orders for each zip code per month.
delivery_dataset['Order date'] = pd.to_datetime(df['Order date'])
delivery_dataset['Delivery date'] = pd.to_datetime(df['Delivery date'])
delivery_dataset['Month'] = delivery_dataset['Delivery date'].dt.month
orders_per_zipcode = delivery_dataset.groupby(['Zipcode', 'Month'])['Zipcode'].count()
print(orders_per_zipcode)
#2. What percentage of customers return after the first order within a week, within a month, within 3 months, 6 months, and year.
from datetime import datetime
customer_ids = delivery_dataset['Customer'].unique()
def wk_diff(date1, date2):
return abs((date2 - date1).days) / 7
def return_perc(time_frame):
count = 0
for customer_id in customer_ids:
customer_order = delivery_dataset[delivery_dataset['Customer'] == customer_id]
first_order_date = delivery_dataset[Order date].min()
orders_within_time_frame = customer_order[(customer_order['Order date'] > first_order_date) & (wk_diff(first_order_date, customer_order['Order date']) <= time_frame)]1 1