Use Historical Sales Data to Predict Demand for Perishable Items
Demand forecasting is crucial for managing perishable goods in grocery stores. Using historical sales data, you can forecast future demand for items like dairy, meat, and produce. By leveraging Python, pandas, and machine learning models, grocery stores can predict when and how much stock to order, reducing waste and ensuring that customers can always find fresh products.
Code Example:
import pandas as pd
from sklearn.linear_model import LinearRegression
import numpy as np
# Load historical sales data (date, product_id, sales_quantity)
sales_data = pd.read_csv('sales_data_perishables.csv', parse_dates=['date'])
# Prepare data: group by product and date to calculate weekly demand
sales_data['week'] = sales_data['date'].dt.week
weekly_sales = sales_data.groupby(['product_id', 'week'])['sales_quantity'].sum().reset_index()
# Train a linear regression model to forecast demand
model = LinearRegression()
# Forecast demand for each product
forecasted_demand = []
for product_id in weekly_sales['product_id'].unique():
product_data = weekly_sales[weekly_sales['product_id'] == product_id]
X = np.array(product_data['week']).reshape(-1, 1)
y = product_data['sales_quantity'].values
model.fit(X, y)
# Predict future demand (next week)
future_demand = model.predict(np.array([[product_data['week'].max() + 1]]))
forecasted_demand.append((product_id, future_demand[0]))
# Save forecasted demand
forecast_df = pd.DataFrame(forecasted_demand, columns=['product_id', 'forecasted_demand'])
forecast_df.to_csv('forecasted_demand.csv', index=False)
print("Demand forecasting completed and saved.")
In this Python script, we load historical sales data, preprocess it to calculate weekly sales quantities, and then use a linear regression model to predict demand for the upcoming week. This allows grocery stores to forecast demand and make data-driven decisions on inventory orders.
Automate Stock Level Adjustments to Reduce Waste and Improve Freshness
With the help of demand forecasting, grocery stores can automate stock level adjustments. By predicting demand for perishable goods, they can ensure they order the right amount of stock without overstocking, which reduces waste and improves product freshness. Using pandas and Python, you can automate the entire process from forecasting to stock management.
Code Example:
# Load forecasted demand and current stock data
forecasted_demand = pd.read_csv('forecasted_demand.csv')
stock_data = pd.read_csv('current_stock.csv')
# Merge forecasted demand with current stock data
merged_data = pd.merge(stock_data, forecasted_demand, on='product_id')
# Adjust stock levels based on forecasted demand
merged_data['adjusted_stock'] = merged_data['current_stock'] - merged_data['forecasted_demand']
# Set minimum stock level to prevent understocking
merged_data['adjusted_stock'] = merged_data['adjusted_stock'].apply(lambda x: max(x, 10)) # minimum of 10 units
# Save adjusted stock levels
merged_data[['product_id', 'adjusted_stock']].to_csv('adjusted_stock_levels.csv', index=False)
print("Stock level adjustments completed and saved.")
This script automates stock adjustments by calculating the difference between forecasted demand and current stock levels. It ensures stock levels are updated in real-time to minimize waste and maintain product freshness.
Optimize Purchasing Decisions with Predictive Analytics
With the insights from demand forecasting and automated stock level adjustments, grocery stores can make better purchasing decisions. By analyzing historical trends and forecasted demand, stores can optimize their orders and reduce waste, while also ensuring they have enough stock to meet customer needs.
Code Example for Optimized Purchasing:
# Load forecasted demand and pricing data
forecasted_demand = pd.read_csv('forecasted_demand.csv')
pricing_data = pd.read_csv('product_pricing.csv')
# Calculate the purchasing cost based on demand and price per unit
merged_data = pd.merge(forecasted_demand, pricing_data, on='product_id')
merged_data['purchasing_cost'] = merged_data['forecasted_demand'] * merged_data['price_per_unit']
# Filter products with the highest purchasing cost to prioritize buying decisions
purchasing_priority = merged_data.sort_values(by='purchasing_cost', ascending=False)
# Save prioritized purchasing decisions
purchasing_priority[['product_id', 'forecasted_demand', 'purchasing_cost']].to_csv('optimized_purchasing.csv', index=False)
print("Optimized purchasing decisions saved.")
In this Python script, we calculate the purchasing cost for each product based on its forecasted demand and price per unit. The result is a prioritized list of products, enabling grocery stores to make informed purchasing decisions that reduce waste while ensuring enough stock to meet demand.
Why Choose Lillqvist Strat?
At Lillqvist Strat, we specialize in automating demand forecasting and stock level adjustments for grocery stores using Python, pandas, and MongoDB. Our solutions:
- Predict demand for perishable goods using historical sales data and machine learning.
- Automate stock management to reduce waste and improve product freshness.
- Optimize purchasing decisions with predictive analytics, ensuring efficient use of resources.
Contact Lillqvist Strat today to implement a data-driven, automated solution for demand forecasting and stock optimization in your grocery store.

Lillqvist Strat consults on business developement, software projects, automation, SOPs, analytical tools and more.
Contact me today to get started on our journey to higher profits, more revenue and happier employees!
Go to Contact now