Sales Data Analysis
Python tutorial Β· PySpark.in
Build a complete data analysis project that loads real data from CSV, cleans the data, analyzes sales, and generates insights.
π This is portfolio-level work (very important for data analyst roles)
Basic Structure
- Pandas β data handling
- Data Cleaning β fix missing/duplicate data
- Aggregation β total, average, grouping
- Filtering & Sorting β insights
Sample Dataset (sales_data.csv)
Product,Category,Sales
Laptop,Electronics,50000
Phone,Electronics,30000
Shirt,Clothing,2000
Laptop,Electronics,45000
Shoes,Clothing,3000
Phone,Electronics,35000
```
import pandas as pd
# Step 1: Create sample sales data
data = {
"Product": ["Laptop", "Mobile", "Laptop", "Tablet", "Mobile", "Headphones", "Tablet"],
"Category": ["Electronics", "Electronics", "Electronics", "Electronics", "Electronics", "Accessories", "Electronics"],
"Sales": [50000, 30000, 50000, None, 45000, 12000, 25000]
}
sample_df = pd.DataFrame(data)
# Step 2: Save sample data to CSV
sample_df.to_csv("sales_data.csv", index=False)
# Step 3: Load data
df = pd.read_csv("sales_data.csv")
# Step 4: Data cleaning
df = df.drop_duplicates()
df["Sales"] = df["Sales"].fillna(df["Sales"].mean())
# Step 5: Basic analysis
print("Total Sales:", df["Sales"].sum())
print("Average Sales:", df["Sales"].mean())
print("Max Sale:", df["Sales"].max())
# Step 6: Sales by category
category_sales = df.groupby("Category")["Sales"].sum()
print("\nSales by Category:")
print(category_sales)
# Step 7: Best selling product
product_sales = df.groupby("Product")["Sales"].sum()
print("\nBest Product:", product_sales.idxmax())
# Step 8: Filter high sales
high_sales = df[df["Sales"] > 30000]
print("\nHigh Sales Records:")
print(high_sales)
# Step 9: Sort data
sorted_data = df.sort_values(by="Sales", ascending=False)
print("\nSorted Data:")
print(sorted_data)
```
π§ Explanation
1. Data Loading: CSV file is loaded into a DataFrame
2. Data Cleaning: Removes duplicate rows, fills missing values with average
3. Basic Metrics: Total, average, and max sales calculated
4. Grouping (Core Skill ): Sales aggregated by category and product
5. Insights Extraction: Best-selling product identified, high-value sales filtered
6. Sorting: Data arranged to highlight top records
π Key Insights You Can Extract
- Which category generates the most revenue
- Which product performs best
- High-value transactions
- Overall sales trends
More Python tutorials
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- Python Data Types Explained β A Beginnerβs Guide
- test
All tutorials Β· Try the free PySpark compiler Β· Practice challenges