LearnPy Hub
← Pandas: Cleaning & Preprocessing problems
Hard

Messy Rows to a Clean Table

Rows are name,age,city,score as text and may be messy. Clean them in this order: strip spaces and title-case name and city; turn age and score into numbers with pd.to_numeric(errors='coerce'); drop exact duplicate rows; fill missing age with the median age and missing score with the mean score (rounded to 1 decimal); fill missing city with the most common city (alphabetically first if tied, Unknown if none). Print the clean rows as name age city score (age as a whole number, score with 1 decimal), then the number of rows.

Input

Lines 'name,age,city,score', then END.

Output

Lines.

Sample 1

Input
 mim ,15,Dhaka,80
RAFI,abc,dhaka ,70
Sara,17,Sylhet,
 mim ,15,Dhaka,80
Tom,16,,95
Nadia,,Khulna,60
END
Output
Mim 15 Dhaka 80.0
Rafi 16 Dhaka 70.0
Sara 17 Sylhet 76.2
Tom 16 Dhaka 95.0
Nadia 16 Khulna 60.0
5 rows

Sample 2

Input
A,10,X,1
END
Output
A 10 X 1.0
1 rows
solution.py
Output appears here
Messy Rows to a Clean Table · Python practice problem · LearnPy Hub