! pip install streamlit
!pip install altair
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings as wrn
import streamlit as st
import altair as alt
'ignore', category = DeprecationWarning)
wrn.filterwarnings('ignore', category = FutureWarning)
wrn.filterwarnings('ignore', category = UserWarning)
wrn.filterwarnings(
= pd.read_excel("//kaggle//input//fenix-shipping-data//bi5EoWE9QkiqEMz37MceAw_2edba123616f40909cb8896b374a31a1_Fenix-Shipping-Data.xlsx") df
# Extract year from order_date and calculate yearly freight costs
= df.groupby(df['order_date'].dt.year)['freight'].sum()
yearly_freight_costs
# Creating the Yearly Freight Costs line chart
=(10, 6))
plt.figure(figsize='line', marker='o', linestyle='-', color='blue')
yearly_freight_costs.plot(kind'Yearly Freight Costs')
plt.title('Year')
plt.xlabel('Total Freight Cost')
plt.ylabel(True)
plt.grid(
plt.show()
# Calculate total freight costs by country and select the top 5 countries
= df.groupby('country')['freight'].sum().sort_values(ascending=False).head(5)
top_countries_freight
# Creating the Top 5 Countries by Freight Cost pie chart
=(8, 8))
plt.figure(figsize='pie', autopct='%1.1f%%', startangle=140, colors=plt.cm.Set3.colors)
top_countries_freight.plot(kind'Top 5 Countries by Freight Cost')
plt.title('') # Hide the y-label
plt.ylabel(
plt.show()
# Creating the Freight Cost Distribution by Order Size histogram
=(10, 6))
plt.figure(figsize'freight'].plot(kind='hist', bins=30, color='green', edgecolor='black')
df['Freight Cost Distribution by Order Size')
plt.title('Freight Cost')
plt.xlabel('Number of Orders')
plt.ylabel(True)
plt.grid(
plt.show()
# Hypothetical sales data by product category
= ['Electronics', 'Clothing', 'Home Goods', 'Books', 'Groceries']
product_categories = [20000, 15000, 12000, 8000, 10000]
sales_volumes
# Create a pie chart
=(10, 7))
plt.figure(figsize=product_categories, autopct='%1.1f%%', startangle=140, colors=plt.cm.tab20.colors)
plt.pie(sales_volumes, labels'Total Sales by Product Category')
plt.title( plt.show()
# Convert order_date to datetime if not already in that format
'order_date'] = pd.to_datetime(df['order_date'])
df[
# Calculate total freight costs
= df['freight'].sum()
total_freight
# Analyze sales (freight) over time - monthly
= df.set_index('order_date')['freight'].resample('M').sum()
monthly_sales
total_freight, monthly_sales
(207306.09999999998,
order_date
1996-07-31 4000.88
1996-08-31 4348.43
1996-09-30 3307.37
1996-10-31 5423.29
1996-11-30 5985.35
1996-12-31 9006.21
1997-01-31 7022.50
1997-02-28 5099.44
1997-03-31 6617.18
1997-04-30 9977.39
1997-05-31 12271.50
1997-06-30 5514.03
1997-07-31 8621.37
1997-08-31 9686.56
1997-09-30 10934.76
1997-10-31 14047.60
1997-11-30 6040.46
1997-12-31 10959.28
1998-01-31 19027.55
1998-02-28 10541.08
1998-03-31 16112.59
1998-04-30 20186.53
1998-05-31 2574.75
Freq: ME, Name: freight, dtype: float64)
# Aggregate data to count orders by country
= df.groupby('country').size().reset_index(name='order_count')
orders_by_country
# Create pie chart
= plt.subplots()
fig, ax 'order_count'], labels=orders_by_country['country'], autopct='%1.1f%%', startangle=90)
ax.pie(orders_by_country['equal') # Equal aspect ratio ensures that pie is drawn as a circle.
ax.axis(
# Display the chart
'Distribution of Orders by Country')
st.title( st.pyplot(fig)
DeltaGenerator()
# Sidebar filters
'Filters')
st.sidebar.header(= st.sidebar.date_input("Date range", [])
date_range = st.sidebar.multiselect('Ship Via', options=df['ship_via'].unique())
ship_via
# Filter the data based on selections
= df.copy()
filtered_data if date_range:
= filtered_data[(filtered_data['order_date'] >= date_range[0]) & (filtered_data['order_date'] <= date_range[1])]
filtered_data if ship_via:
= filtered_data[filtered_data['ship_via'].isin(ship_via)]
filtered_data
# Group data by region
= filtered_data.groupby('region')['order_id'].nunique().reset_index()
orders_per_region
# Chart: Orders per Region
= alt.Chart(orders_per_region).mark_bar().encode(
chart ='region:N',
x='order_id:Q',
y=['region', 'order_id']
tooltip=600, height=400, title='Orders per Region')
).properties(width
=True) st.altair_chart(chart, use_container_width
DeltaGenerator()