-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpython-pandas.html
More file actions
642 lines (543 loc) · 23.8 KB
/
python-pandas.html
File metadata and controls
642 lines (543 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="images/logo.png">
<link rel="apple-touch-icon" href="images/logo.png">
<title>Python Pandas - Developer Toolkit | DevDunia</title>
<meta name="description" content="Data manipulation, CSV handling, and DataFrame operations. Free online Python Pandas examples for developers.">
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #0f172a 100%);
}
</style>
</head>
<body class="h-screen lg:ml-72">
<!-- Background: Consistent Subtle Dark Gradient -->
<div class="fixed inset-0 -z-10 bg-gradient-to-br from-gray-900 via-slate-900 to-gray-900"></div>
<!-- Main Container -->
<div class="relative z-10 container mx-auto px-4 pt-16 pb-24 sm:px-6 lg:px-8">
<!-- Page Header -->
<div class="text-center mb-12">
<h1 class="text-3xl sm:text-4xl font-bold mb-4 tracking-tight
text-transparent bg-clip-text bg-gradient-to-r from-emerald-400 to-teal-500">
Python Pandas
</h1>
<p class="text-lg text-gray-400 max-w-2xl mx-auto">
Data manipulation, CSV handling, and DataFrame operations.
</p>
</div>
<!-- Tool Container -->
<div class="max-w-4xl mx-auto">
<div class="bg-slate-800/70 backdrop-blur-md rounded-lg shadow-lg border border-slate-700/60 p-6">
<!-- Operation Type Selection -->
<div class="mb-6">
<label class="block text-sm font-medium text-gray-300 mb-3">Pandas Operation</label>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
<button class="operation-btn px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-white text-sm rounded-lg transition-colors duration-200" data-operation="basic">
Basic Operations
</button>
<button class="operation-btn px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white text-sm rounded-lg transition-colors duration-200" data-operation="csv">
CSV Handling
</button>
<button class="operation-btn px-4 py-2 bg-purple-600 hover:bg-purple-700 text-white text-sm rounded-lg transition-colors duration-200" data-operation="filtering">
Data Filtering
</button>
<button class="operation-btn px-4 py-2 bg-yellow-600 hover:bg-yellow-700 text-white text-sm rounded-lg transition-colors duration-200" data-operation="grouping">
Grouping & Aggregation
</button>
<button class="operation-btn px-4 py-2 bg-red-600 hover:bg-red-700 text-white text-sm rounded-lg transition-colors duration-200" data-operation="merging">
Data Merging
</button>
<button class="operation-btn px-4 py-2 bg-cyan-600 hover:bg-cyan-700 text-white text-sm rounded-lg transition-colors duration-200" data-operation="analysis">
Data Analysis
</button>
</div>
</div>
<!-- Generate Button -->
<div class="mb-6">
<button id="generate-btn" class="px-6 py-3 bg-emerald-600 hover:bg-emerald-700 text-white font-medium rounded-lg transition-colors duration-200 flex items-center space-x-2">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path>
</svg>
<span>Generate Pandas Code</span>
</button>
</div>
<!-- Generated Code -->
<div id="code-section" class="hidden">
<h3 class="text-lg font-semibold text-gray-300 mb-4">Generated Pandas Code</h3>
<div class="relative">
<textarea
id="generated-code"
rows="25"
class="w-full px-4 py-3 bg-slate-700/50 border border-slate-600 rounded-lg text-gray-200 font-mono text-sm resize-none"
readonly
></textarea>
<!-- Copy Button -->
<button id="copy-code-btn" class="absolute top-2 right-2 px-3 py-1 bg-slate-600 hover:bg-slate-700 text-white text-sm rounded transition-colors duration-200 flex items-center space-x-1">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v8a2 2 0 01-2 2h-2m-6-4l2 2m0 0l2-2m-2 2V10"></path>
</svg>
<span>Copy</span>
</button>
</div>
</div>
<!-- Requirements -->
<div class="mt-8 p-4 bg-slate-700/30 rounded-lg border border-slate-600/50">
<h3 class="text-lg font-semibold text-cyan-400 mb-3">Requirements</h3>
<div class="bg-slate-600/50 p-3 rounded border border-slate-500">
<pre class="text-cyan-300 font-mono text-sm">pandas==2.1.0
numpy==1.24.3
matplotlib==3.7.2</pre>
</div>
</div>
<!-- Common Operations -->
<div class="mt-6 p-4 bg-slate-700/30 rounded-lg border border-slate-600/50">
<h3 class="text-lg font-semibold text-cyan-400 mb-3">Common Pandas Operations</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
<div>
<p class="text-gray-300 font-medium mb-2">Data Loading:</p>
<p class="text-gray-400">pd.read_csv(), pd.read_excel(), pd.read_json()</p>
</div>
<div>
<p class="text-gray-300 font-medium mb-2">Data Inspection:</p>
<p class="text-gray-400">df.head(), df.info(), df.describe()</p>
</div>
<div>
<p class="text-gray-300 font-medium mb-2">Data Cleaning:</p>
<p class="text-gray-400">df.dropna(), df.fillna(), df.drop_duplicates()</p>
</div>
<div>
<p class="text-gray-300 font-medium mb-2">Data Selection:</p>
<p class="text-gray-400">df.loc[], df.iloc[], df.query()</p>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Footer -->
<footer class="text-center text-gray-500 mt-10 pb-6">
© 2025 DevDunia – Free Developer Tools by <a href="https://echobash.com" class="text-blue-500 hover:underline" target="_blank">echobash</a>
</footer>
<script>
document.addEventListener('DOMContentLoaded', function() {
const operationBtns = document.querySelectorAll('.operation-btn');
const generateBtn = document.getElementById('generate-btn');
const codeSection = document.getElementById('code-section');
const generatedCode = document.getElementById('generated-code');
const copyCodeBtn = document.getElementById('copy-code-btn');
let selectedOperation = 'basic';
// Operation selection
operationBtns.forEach(btn => {
btn.addEventListener('click', function() {
selectedOperation = this.getAttribute('data-operation');
// Update button states
operationBtns.forEach(b => b.classList.remove('ring-2', 'ring-emerald-400'));
this.classList.add('ring-2', 'ring-emerald-400');
});
});
// Set initial operation
document.querySelector('[data-operation="basic"]').click();
// Generate code
generateBtn.addEventListener('click', function() {
const code = generatePandasCode();
generatedCode.value = code;
codeSection.classList.remove('hidden');
});
// Copy code
copyCodeBtn.addEventListener('click', function() {
generatedCode.select();
document.execCommand('copy');
// Visual feedback
const originalText = copyCodeBtn.innerHTML;
copyCodeBtn.innerHTML = '<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg><span>Copied!</span>';
copyCodeBtn.classList.add('bg-green-600');
setTimeout(() => {
copyCodeBtn.innerHTML = originalText;
copyCodeBtn.classList.remove('bg-green-600');
}, 2000);
});
function generatePandasCode() {
switch (selectedOperation) {
case 'basic':
return generateBasicOperations();
case 'csv':
return generateCSVHandling();
case 'filtering':
return generateDataFiltering();
case 'grouping':
return generateGroupingAggregation();
case 'merging':
return generateDataMerging();
case 'analysis':
return generateDataAnalysis();
default:
return generateBasicOperations();
}
}
function generateBasicOperations() {
return `import pandas as pd
import numpy as np
# Create a sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Age': [25, 30, 35, 28, 32],
'City': ['New York', 'London', 'Tokyo', 'Paris', 'Sydney'],
'Salary': [50000, 60000, 70000, 55000, 65000]
}
df = pd.DataFrame(data)
# Basic DataFrame operations
print("DataFrame Info:")
print(df.info())
print("\\nFirst 3 rows:")
print(df.head(3))
print("\\nDataFrame shape:", df.shape)
print("\\nColumn names:", df.columns.tolist())
# Basic statistics
print("\\nBasic Statistics:")
print(df.describe())
# Accessing data
print("\\nAccessing specific columns:")
print(df['Name'])
print("\\nAccessing multiple columns:")
print(df[['Name', 'Age']])
# Adding new column
df['Bonus'] = df['Salary'] * 0.1
print("\\nDataFrame with bonus column:")
print(df)
# Sorting
print("\\nSorted by Age:")
print(df.sort_values('Age'))
# Filtering
print("\\nPeople older than 30:")
print(df[df['Age'] > 30])`;
}
function generateCSVHandling() {
return `import pandas as pd
# Reading CSV files
# df = pd.read_csv('data.csv')
# df = pd.read_csv('data.csv', sep=';') # Custom separator
# df = pd.read_csv('data.csv', header=0) # Specify header row
# df = pd.read_csv('data.csv', index_col=0) # Use first column as index
# Create sample data for demonstration
data = {
'ID': [1, 2, 3, 4, 5],
'Product': ['Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Headphones'],
'Price': [999.99, 29.99, 79.99, 299.99, 149.99],
'Category': ['Electronics', 'Accessories', 'Accessories', 'Electronics', 'Accessories'],
'Stock': [10, 50, 25, 15, 30]
}
df = pd.DataFrame(data)
# Writing to CSV
df.to_csv('products.csv', index=False)
print("Data written to products.csv")
# Reading from CSV
df_read = pd.read_csv('products.csv')
print("\\nData read from CSV:")
print(df_read)
# CSV with different options
# df.to_csv('products.csv', index=False, sep=';') # Custom separator
# df.to_csv('products.csv', index=False, encoding='utf-8') # Custom encoding
# Handling missing values in CSV
df_with_nulls = df.copy()
df_with_nulls.loc[1, 'Price'] = None
df_with_nulls.loc[3, 'Stock'] = None
print("\\nDataFrame with missing values:")
print(df_with_nulls)
# Save with missing values
df_with_nulls.to_csv('products_with_nulls.csv', index=False)
# Read and handle missing values
df_clean = pd.read_csv('products_with_nulls.csv')
print("\\nDataFrame with missing values handled:")
print(df_clean.fillna(0)) # Fill with 0
print("\\nOr drop rows with missing values:")
print(df_clean.dropna())`;
}
function generateDataFiltering() {
return `import pandas as pd
import numpy as np
# Create sample data
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Henry'],
'Age': [25, 30, 35, 28, 32, 45, 29, 38],
'Department': ['IT', 'HR', 'IT', 'Finance', 'IT', 'HR', 'Finance', 'IT'],
'Salary': [50000, 60000, 70000, 55000, 65000, 80000, 58000, 72000],
'Experience': [2, 5, 8, 3, 6, 15, 4, 10]
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# Basic filtering
print("\\n1. Filter by single condition:")
it_employees = df[df['Department'] == 'IT']
print(it_employees)
print("\\n2. Filter by multiple conditions:")
senior_it = df[(df['Department'] == 'IT') & (df['Experience'] > 5)]
print(senior_it)
print("\\n3. Filter by salary range:")
high_salary = df[(df['Salary'] >= 60000) & (df['Salary'] <= 80000)]
print(high_salary)
# Using query method
print("\\n4. Using query method:")
young_employees = df.query('Age < 30 and Department == "IT"')
print(young_employees)
# Using isin for multiple values
print("\\n5. Filter by multiple department values:")
selected_depts = df[df['Department'].isin(['IT', 'Finance'])]
print(selected_depts)
# String filtering
print("\\n6. Filter by name containing 'a':")
names_with_a = df[df['Name'].str.contains('a', case=False)]
print(names_with_a)
# Using loc for more complex filtering
print("\\n7. Using loc for complex filtering:")
complex_filter = df.loc[(df['Age'] > 30) & (df['Salary'] > 60000), ['Name', 'Department', 'Salary']]
print(complex_filter)
# Filtering with null values (if any)
print("\\n8. Handling null values:")
df_with_nulls = df.copy()
df_with_nulls.loc[2, 'Salary'] = None
print("DataFrame with nulls:")
print(df_with_nulls)
print("\\nNon-null salaries:")
print(df_with_nulls[df_with_nulls['Salary'].notna()])`;
}
function generateGroupingAggregation() {
return `import pandas as pd
import numpy as np
# Create sample sales data
data = {
'Date': pd.date_range('2023-01-01', periods=20, freq='D'),
'Product': ['Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Laptop', 'Mouse',
'Keyboard', 'Monitor', 'Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Laptop', 'Mouse', 'Keyboard', 'Monitor'],
'Category': ['Electronics', 'Accessories', 'Accessories', 'Electronics', 'Electronics', 'Accessories', 'Accessories', 'Electronics',
'Electronics', 'Accessories', 'Accessories', 'Electronics', 'Electronics', 'Accessories', 'Accessories', 'Electronics',
'Electronics', 'Accessories', 'Accessories', 'Electronics'],
'Sales': [1000, 50, 100, 500, 1200, 60, 110, 550, 1100, 45, 95, 480, 1300, 55, 105, 520, 1150, 65, 115, 510],
'Quantity': [1, 10, 5, 2, 1, 12, 6, 2, 1, 9, 5, 2, 1, 11, 5, 2, 1, 13, 6, 2],
'Region': ['North', 'South', 'East', 'West', 'North', 'South', 'East', 'West', 'North', 'South',
'East', 'West', 'North', 'South', 'East', 'West', 'North', 'South', 'East', 'West']
}
df = pd.DataFrame(data)
print("Sample Sales Data:")
print(df.head(10))
# Basic grouping
print("\\n1. Group by Product and get total sales:")
product_sales = df.groupby('Product')['Sales'].sum()
print(product_sales)
print("\\n2. Group by Category and get multiple aggregations:")
category_stats = df.groupby('Category').agg({
'Sales': ['sum', 'mean', 'count'],
'Quantity': 'sum'
})
print(category_stats)
# Multiple grouping
print("\\n3. Group by Category and Region:")
category_region = df.groupby(['Category', 'Region'])['Sales'].sum()
print(category_region)
# Custom aggregation functions
print("\\n4. Custom aggregation:")
custom_agg = df.groupby('Product').agg({
'Sales': ['sum', 'mean', 'std'],
'Quantity': ['sum', 'max']
})
print(custom_agg)
# Using transform for group-wise operations
print("\\n5. Add group mean to each row:")
df['Group_Mean'] = df.groupby('Category')['Sales'].transform('mean')
print(df[['Product', 'Category', 'Sales', 'Group_Mean']])
# Using apply for custom functions
print("\\n6. Custom function with apply:")
def sales_range(group):
return group['Sales'].max() - group['Sales'].min()
sales_ranges = df.groupby('Category').apply(sales_range)
print("Sales range by category:")
print(sales_ranges)
# Pivot tables
print("\\n7. Pivot table - Sales by Category and Region:")
pivot_table = df.pivot_table(values='Sales', index='Category', columns='Region', aggfunc='sum')
print(pivot_table)
# Cross tabulation
print("\\n8. Cross tabulation:")
crosstab = pd.crosstab(df['Category'], df['Region'])
print(crosstab)`;
}
function generateDataMerging() {
return `import pandas as pd
# Create sample datasets
employees = pd.DataFrame({
'Employee_ID': [1, 2, 3, 4, 5],
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Department_ID': [10, 20, 10, 30, 20],
'Salary': [50000, 60000, 70000, 55000, 65000]
})
departments = pd.DataFrame({
'Department_ID': [10, 20, 30, 40],
'Department_Name': ['IT', 'HR', 'Finance', 'Marketing'],
'Manager': ['John', 'Jane', 'Mike', 'Sarah']
})
projects = pd.DataFrame({
'Employee_ID': [1, 2, 1, 3, 4, 2],
'Project_Name': ['Project A', 'Project B', 'Project C', 'Project A', 'Project D', 'Project C'],
'Hours': [40, 35, 20, 45, 30, 25]
})
print("Employees DataFrame:")
print(employees)
print("\\nDepartments DataFrame:")
print(departments)
print("\\nProjects DataFrame:")
print(projects)
# Inner join
print("\\n1. Inner join - Employees with Departments:")
inner_join = pd.merge(employees, departments, on='Department_ID', how='inner')
print(inner_join)
# Left join
print("\\n2. Left join - All employees with their departments:")
left_join = pd.merge(employees, departments, on='Department_ID', how='left')
print(left_join)
# Right join
print("\\n3. Right join - All departments with employees:")
right_join = pd.merge(employees, departments, on='Department_ID', how='right')
print(right_join)
# Outer join
print("\\n4. Outer join - All records:")
outer_join = pd.merge(employees, departments, on='Department_ID', how='outer')
print(outer_join)
# Multiple merges
print("\\n5. Multiple merges - Employees, Departments, and Projects:")
multi_merge = pd.merge(
pd.merge(employees, departments, on='Department_ID', how='left'),
projects, on='Employee_ID', how='left'
)
print(multi_merge)
# Merge with different column names
print("\\n6. Merge with different column names:")
employees_alt = employees.copy()
employees_alt.rename(columns={'Department_ID': 'Dept_ID'}, inplace=True)
merge_different_cols = pd.merge(employees_alt, departments,
left_on='Dept_ID', right_on='Department_ID', how='left')
print(merge_different_cols)
# Concatenation
print("\\n7. Concatenating DataFrames:")
new_employees = pd.DataFrame({
'Employee_ID': [6, 7],
'Name': ['Frank', 'Grace'],
'Department_ID': [10, 30],
'Salary': [58000, 62000]
})
concatenated = pd.concat([employees, new_employees], ignore_index=True)
print("Concatenated employees:")
print(concatenated)
# Append (deprecated but still works)
print("\\n8. Using append:")
appended = employees.append(new_employees, ignore_index=True)
print("Appended employees:")
print(appended)`;
}
function generateDataAnalysis() {
return `import pandas as pd
import numpy as np
# Create comprehensive sample data
np.random.seed(42)
data = {
'Date': pd.date_range('2023-01-01', periods=100, freq='D'),
'Product': np.random.choice(['Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Headphones'], 100),
'Category': np.random.choice(['Electronics', 'Accessories'], 100),
'Sales': np.random.normal(1000, 300, 100).round(2),
'Quantity': np.random.randint(1, 20, 100),
'Region': np.random.choice(['North', 'South', 'East', 'West'], 100),
'Customer_Rating': np.random.uniform(1, 5, 100).round(1)
}
df = pd.DataFrame(data)
print("Sample Data Analysis:")
print(df.head())
print(f"\\nDataset shape: {df.shape}")
# Basic statistical analysis
print("\\n1. Descriptive Statistics:")
print(df.describe())
print("\\n2. Data types and missing values:")
print(df.info())
# Correlation analysis
print("\\n3. Correlation Matrix:")
numeric_cols = df.select_dtypes(include=[np.number]).columns
correlation_matrix = df[numeric_cols].corr()
print(correlation_matrix)
# Time series analysis
print("\\n4. Time Series Analysis:")
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
# Monthly sales trend
monthly_sales = df['Sales'].resample('M').sum()
print("Monthly Sales Trend:")
print(monthly_sales)
# Moving average
df['Sales_MA_7'] = df['Sales'].rolling(window=7).mean()
print("\\n7-day Moving Average (first 10 days):")
print(df['Sales_MA_7'].head(10))
# Categorical analysis
print("\\n5. Categorical Analysis:")
print("Sales by Category:")
category_analysis = df.groupby('Category').agg({
'Sales': ['sum', 'mean', 'count'],
'Customer_Rating': 'mean'
})
print(category_analysis)
# Top performing products
print("\\n6. Top 5 Products by Total Sales:")
top_products = df.groupby('Product')['Sales'].sum().sort_values(ascending=False).head()
print(top_products)
# Regional analysis
print("\\n7. Regional Performance:")
regional_analysis = df.groupby('Region').agg({
'Sales': ['sum', 'mean'],
'Customer_Rating': 'mean',
'Quantity': 'sum'
})
print(regional_analysis)
# Outlier detection
print("\\n8. Outlier Detection (using IQR method):")
Q1 = df['Sales'].quantile(0.25)
Q3 = df['Sales'].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
outliers = df[(df['Sales'] < lower_bound) | (df['Sales'] > upper_bound)]
print(f"Number of outliers: {len(outliers)}")
print("Outliers:")
print(outliers[['Product', 'Sales', 'Region']])
# Data quality checks
print("\\n9. Data Quality Checks:")
print(f"Missing values: {df.isnull().sum().sum()}")
print(f"Duplicate rows: {df.duplicated().sum()}")
print(f"Unique products: {df['Product'].nunique()}")
print(f"Date range: {df.index.min()} to {df.index.max()}")
# Performance metrics
print("\\n10. Performance Metrics:")
total_sales = df['Sales'].sum()
avg_rating = df['Customer_Rating'].mean()
total_quantity = df['Quantity'].sum()
print(f"Total Sales: \${total_sales:,.2f}")
print(f"Average Customer Rating: {avg_rating:.2f}")
print(f"Total Quantity Sold: {total_quantity:,}")
print(f"Average Sales per Transaction: \${total_sales/len(df):.2f}")`;
}
});
</script>
<!-- Include Final Sidebar -->
<div id="sidebar-container"></div>
<script>
// Load final_sidebar.html content
fetch('final_sidebar.html')
.then(response => response.text())
.then(html => {
document.getElementById('sidebar-container').innerHTML = html;
})
.catch(error => {
console.error('Error loading sidebar:', error);
});
</script>
</body>
</html>