topskill.dev
CoursesTracks
0
All courses/Introduction to Pandas: Community Data DeskClean the sale rows
8. Build a Market Report
3 / 12
← Introduction to Pandas: Community Data Desk
○A desk for labeled data○Import pandas for a stall card○Exercise: Make a count card○Read the default index○Exercise: Number the arrivals○Name the labels○Exercise: Label a shift○Pick a label or a position○Exercise: Find two counts○Add counts by label○Exercise: Combine two shifts✦Module Quiz
○Build a market table○Exercise: Assemble two tables○Inspect labels and dtypes○Exercise: Read the table card○Preview and summarize rows○Exercise: Check the new delivery○Select a column○Exercise: Pack a report view○Find a row and column○Exercise: Look up table cells○Add a calculated column○Exercise: Calculate market revenue✦Module Quiz
○Choose columns for a stall card○Exercise: make a vendor contact sheet○Find rows by label and position○Exercise: inspect two delivery windows○Filter stalls with a comparison mask○Exercise: shortlist ready orders○Combine two selection rules○Exercise: build two volunteer queues○Match a list of categories○Exercise: list invited workshop types○Order a stall leaderboard○Exercise: rank pickup points✦Module Quiz
○Spot missing records before filing○Exercise: audit delivery addresses○Fill an absent attendance count○Exercise: complete inspection ratings○Recognize repeated entries○Exercise: reconcile order revisions○Turn text counts into numbers○Exercise: repair case quantities○Standardize names and column labels○Exercise: standardize workshop registrations○Update selected rows safely○Project: prepare a market log✦Module Quiz
○Stack delivery sheets○Exercise: Stack visit logs○Align column notes○Exercise: Align fees○Merge on a named key○Exercise: Match payments○Guard against duplicate prices○Exercise: Validate badges○Join indexed areas○Exercise: Join fee bands○Mini project: Assemble the market ledger✦Module Quiz
○Count values and rows○Exercise: Summarize zone receipts○Profile values and labels○Exercise: Profile stall visits○Group on two labels○Exercise: Group shift totals○Name grouped results○Exercise: Name category measures○Return a baseline per row○Exercise: Compare visits to a group○Pivot a daily grid○Mini project: Publish the market summary✦Module Quiz
○Read a market CSV○Exercise: load inventory CSV○Parse dates while reading○Exercise: convert sign-up dates○Find the day with dt○Exercise: count visitors by weekday○Read JSON records○Exercise: load a JSON roster○Export CSV without an index○Exercise: round-trip a sales table○Sketch data with text bars○Exercise: chart item totals✦Module Quiz
○Warm-up: read a ticket sheet○Import the two market sheets●Clean the sale rows○Add revenue and sale day○Filter eligible sales○Attach the stall directory○Group neighborhood totals○Export a shareable CSV○Audit the reported rows○Assemble the market report○Exercise: compare two channels✦Module Quiz

Clean the sale rows

The sample repeats sale ID 2 and leaves one unit count blank. Clean these rows before multiplying amounts. The completed import is supplied; make the changes inside clean_sales in main.py.

The starter creates a copy and returns a fresh index. Add one cleaning operation at each task comment, then Run or Submit. Run keeps working while later comments are unfinished. The tests use separate inputs for each operation, so removing duplicates does not require date conversion to be complete.

Keep the first occurrence of each sale_id, even if a later copy looks more usable. Convert sold_at with pd.to_datetime(..., format="%Y-%m-%d", errors="coerce"). These inputs are timezone-free year-month-day dates; a bad date becomes NaT. For each amount column, pd.to_numeric(..., errors="coerce") turns a bad numeric value into a missing value. Neither conversion drops a row by itself.

After conversion, dropna(subset=[...]) removes rows missing a required date or amount. Finally keep positive units and prices of zero or more; a free item is allowed, but a return with negative units is outside this sales report. The supplied copy protects the original sheet for an audit.

Run begins at six raw and six returned rows. After duplicate removal there are five. With every task finished, the clean sale IDs are 1, 2, 4, and 5.

Your Task

  1. Remove repeated sale_id values from cleaned, keeping the first occurrence.
  2. Convert cleaned["sold_at"] to datetimes in year-month-day format, coercing invalid values to NaT.
  3. Convert cleaned["units"] to numbers, coercing invalid values to missing.
  4. Convert cleaned["unit_price"] to numbers, coercing invalid values to missing.
  5. Remove rows missing any of sold_at, units, or unit_price.
  6. Keep only rows with units > 0 and unit_price >= 0.
Hint 1
1 / 1
HINT 1

Assign each operation back to cleaned or its named column. Leave the copy and reset-index return in place.

Files
  • ⋄main.py
Loading editor…
Press Run to execute your code.