Daily customer counts across locations - Panipuri
Description
Code Description: Analyzing Daily Customer Counts Across Multiple Locations This Python code snippet utilizes libraries such as NumPy, pandas, and Matplotlib to simulate and analyze customer footfall data across three different locations over a 30-day period. The primary objective is to generate random customer count data, visualize it, and provide insights into customer behavior in various commercial environments. Key Components of the Code: Library Imports: The code begins by importing necessary libraries: numpy for numerical operations and random number generation. pandas for data manipulation and analysis. matplotlib.pyplot for creating visualizations. Data Simulation: A random seed is set using np.random.seed(42) to ensure that the results are reproducible. The variable days is created as an array representing the days of the month (from 1 to 30). Three locations are defined: "Market," "Mall," and "Station." Random Customer Count Generation: A dictionary named data is initialized with the days of the month. For each location, random customer counts are generated using np.random.randint(50, 200, size=len(days)), which creates an array of random integers between 50 and 200. This simulates variations in customer foot traffic for each location over the 30 days. The resulting data dictionary is then converted into a pandas DataFrame named df. Data Verification: The code prints the first five rows of the DataFrame using print(df.head()). This step ensures that the data has been generated correctly and allows for a quick inspection of the structure. Data Visualization: A line plot is created to visualize daily customer counts across the three locations. The figure size is set to (10, 5) for better visibility. A loop iterates through each location, plotting the corresponding customer counts against the days of the month. Each line is labeled with its respective location name and marked with circular markers for clarity. Labels for the x-axis ("Day") and y-axis ("Number of Customers") are added, along with a legend to identify each location's data series. A title ("Daily Customer Count Across Locations") is assigned to the plot, and a grid is enabled for easier reading of values. Display Plot: Finally, plt.show() is called to render the plot visually.
Files
Steps to reproduce
import numpy as np import pandas as pd import matplotlib.pyplot as plt # Simulated data for 30 days np.random.seed(42) days = np.arange(1, 31) locations = ["Market", "Mall", "Station"] # Generate random customer count data with variations data = {"Day": days} for loc in locations: data[f"{loc}_Customers"] = np.random.randint(50, 200, size=len(days)) df = pd.DataFrame(data) # Ensure all columns exist before plotting print(df.head()) plt.figure(figsize=(10, 5)) for loc in locations: if f"{loc}_Customers" in df.columns: plt.plot(df["Day"].values, df[f"{loc}_Customers"].values, label=f"{loc} Customers", marker="o") plt.xlabel("Day") plt.ylabel("Number of Customers") plt.legend() plt.title("Daily Customer Count Across Locations") plt.grid() plt.show()