Pandas and NumPy · Course lab · about 150 minutes · 6 tasks · marked out of 100, pass at 60
Turn one messy CSV into an analysis someone can rerun
The situation
A chain of pharmacies in Lahore exports its monthly sales as a single messy CSV: inconsistent branch names, prices stored as text with 'Rs', a few blank rows, and dates in two formats. The manager wants three numbers she can trust each month, and an analysis her junior can rerun next month without you.
Clean it with pandas, answer her questions, and make the notebook reproducible.
What you'll be able to show
- Know when to reach for NumPy and when for pandas
- Load a file and understand it with the standard first checks
- Filter, add columns and change values without breaking the row count
- Group and merge correctly, and make the analysis rerun the same way twice
What you need
- Python with pandas and numpy, in Jupyter, Colab or any notebook
- A messy sales CSV you build to the description below
Tasks
-
1Build the mess on purpose
Create a CSV of at least 200 sales rows: columns branch, date, product, quantity, price. Make it realistically dirty: three spellings of one branch, prices like 'Rs 450' as text, five blank rows, and dates in both 2026-03-01 and 01/03/2026 styles.A correct result: The CSV and a note listing the four kinds of mess you put in. -
2Look before you touch
Load it and run the standard checks: shape, head, dtypes, describe, and isna().sum(). Write one sentence about the file based only on what they showed.A correct result: The output of the five checks and your one-sentence summary. -
3Clean it
Drop the blank rows, standardise the branch names, parse both date formats into real dates, and convert price to a number. Keep the raw frame and clean into a copy. Check the row count after each step.A correct result: A clean dataframe, the row count at each step, and a short list of the fixes. -
4Vectorise one calculation
Add a line_total column (quantity times price) with a vectorised operation, not a loop. Then show the same thing done with a Python loop and compare the time on the full file.A correct result: The vectorised column, the loop version, and the two timings. -
5Answer the three questions
Group to answer: revenue by branch this month, the top five products by revenue, and the average basket value per branch. Merge in a branch-region lookup and confirm the row count did not change.A correct result: Three result tables and the before-and-after row count of the merge. -
6Make it rerun
Add at least three assertions (for example no nulls in price, row count within an expected range, revenue positive). Restart the kernel and run everything top to bottom. Confirm the three numbers are identical.A correct result: The assertions, and a note confirming a clean top-to-bottom run gives the same numbers.
What to hand in
The notebook (with the raw CSV), showing the checks, the cleaning, the timed comparison, the three answers and the assertions, plus the three numbers written out for the manager.
How it is marked
| Criterion | Points |
|---|---|
| Messy file built and understood with the standard checks | 15 |
| Cleaning correct with the row count tracked at each step | 25 |
| One calculation vectorised and timed against a loop | 20 |
| Three questions answered and the merge did not change the count | 20 |
| Assertions added and a clean rerun gives identical numbers | 20 |
| Total · pass at 60 | 100 |
صورتحال
لاہور میں فارمیسیوں کی ایک چین اپنی ماہانہ فروخت ایک بکھری ہوئی CSV کے طور پر ایکسپورٹ کرتی ہے: برانچ کے نام مختلف، قیمتیں 'Rs' کے ساتھ متن میں، چند خالی قطاریں، اور تاریخیں دو شکلوں میں۔ منیجر ہر مہینے تین قابلِ بھروسہ اعداد چاہتی ہیں، اور ایسا تجزیہ جو ان کا جونیئر اگلے مہینے آپ کے بغیر دوبارہ چلا سکے۔
اسے pandas سے صاف کریں، ان کے سوالوں کے جواب دیں، اور نوٹ بک کو دوبارہ چلنے کے قابل بنائیں۔
آپ کیا دکھا سکیں گے
- جاننا کہ کب NumPy کی طرف جانا ہے اور کب pandas کی
- فائل لوڈ کرنا اور معیاری پہلے چیکس سے اسے سمجھنا
- قطاروں کی تعداد توڑے بغیر فلٹر کرنا، کالم شامل کرنا اور قدریں بدلنا
- درست گروپ اور merge کرنا، اور تجزیے کو دو بار ایک جیسا چلانا
آپ کو کیا چاہیے
- Python کے ساتھ pandas اور numpy، Jupyter، Colab یا کسی نوٹ بک میں
- نیچے دی گئی تفصیل کے مطابق بنائی گئی ایک بکھری فروخت CSV
کام
-
1جان بوجھ کر بگاڑ بنائیں
کم از کم 200 فروخت قطاروں کی CSV بنائیں: کالم branch، date، product، quantity، price۔ اسے حقیقت پسندانہ طور پر گندا کریں: ایک برانچ کے تین ہجے، 'Rs 450' جیسی قیمتیں متن میں، پانچ خالی قطاریں، اور تاریخیں 2026-03-01 اور 01/03/2026 دونوں شکلوں میں۔درست نتیجہ: CSV اور چار قسم کے بگاڑ کی فہرست کا نوٹ۔ -
2چھونے سے پہلے دیکھیں
اسے لوڈ کریں اور معیاری چیکس چلائیں: shape، head، dtypes، describe، اور isna().sum()۔ صرف جو انہوں نے دکھایا اس کی بنیاد پر فائل کے بارے میں ایک جملہ لکھیں۔درست نتیجہ: پانچ چیکس کا آؤٹ پٹ اور آپ کا ایک جملے کا خلاصہ۔ -
3صاف کریں
خالی قطاریں ہٹائیں، برانچ کے نام یکساں کریں، دونوں تاریخ کی شکلیں اصلی تاریخوں میں بدلیں، اور price کو عدد میں بدلیں۔ خام frame رکھیں اور نقل میں صاف کریں۔ ہر قدم کے بعد قطاروں کی تعداد جانچیں۔درست نتیجہ: صاف dataframe، ہر قدم پر قطاروں کی تعداد، اور درستیوں کی مختصر فہرست۔ -
4ایک حساب vectorise کریں
line_total کالم (quantity ضرب price) ایک vectorised عمل سے لگائیں، لوپ سے نہیں۔ پھر وہی چیز Python لوپ سے کر کے دکھائیں اور پوری فائل پر وقت کا موازنہ کریں۔درست نتیجہ: vectorised کالم، لوپ والا ورژن، اور دونوں اوقات۔ -
5تین سوالوں کے جواب
گروپ کر کے جواب دیں: اس مہینے برانچ کے لحاظ سے آمدنی، آمدنی کے لحاظ سے پانچ پروڈکٹس، اور ہر برانچ کی اوسط باسکٹ قیمت۔ ایک branch-region لُک اپ merge کریں اور تصدیق کریں کہ قطاروں کی تعداد نہیں بدلی۔درست نتیجہ: تین نتائج کی جدولیں اور merge کی پہلے اور بعد کی قطاروں کی تعداد۔ -
6دوبارہ چلنے کے قابل بنائیں
کم از کم تین assertions لگائیں (مثلاً price میں کوئی null نہیں، قطاروں کی تعداد متوقع حد میں، آمدنی مثبت)۔ kernel دوبارہ شروع کریں اور سب کچھ اوپر سے نیچے چلائیں۔ تصدیق کریں کہ تینوں اعداد ایک جیسے ہیں۔درست نتیجہ: assertions، اور ایک نوٹ کہ صاف اوپر سے نیچے چلانے پر وہی اعداد ملتے ہیں۔
کیا جمع کروانا ہے
نوٹ بک (خام CSV کے ساتھ) جو چیکس، صفائی، وقت کا موازنہ، تین جواب اور assertions دکھائے، ساتھ منیجر کے لیے تینوں اعداد لکھے ہوئے۔
نمبر کیسے ملیں گے
| معیار | نمبر |
|---|---|
| بکھری فائل بنی اور معیاری چیکس سے سمجھی گئی | 15 |
| صفائی درست اور ہر قدم پر قطاروں کی تعداد جانچی گئی | 25 |
| ایک حساب vectorise ہوا اور لوپ سے وقت کا موازنہ ہوا | 20 |
| تین سوالوں کے جواب دیے اور merge نے تعداد نہیں بدلی | 20 |
| assertions لگے اور صاف دوبارہ چلانے پر ایک جیسے اعداد ملے | 20 |
| کل · پاس 60 | 100 |
Hand in your lab
Create a free BvLogic ID to hand in your lab, get it marked, and have it on your certificate.
Create your BvLogic IDAlready have one? Sign in and this course will be added to it.