נבנה על ידיBuilt by Ariel Yohimovich

PythonPython
24
חלקPart 4: נתונים ורשתData & the web כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

pandas למתחיליםpandas for beginners

DataFrame, סינון, קיבוץ וסטטיסטיקה.

DataFrame, filtering, grouping and statistics.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

1. מה מחזיר df["pages"]?What does df["pages"] return?

2. איך מסננים ספרים מעל 300 עמודים עם דירוג 5?How do you filter books over 300 pages with rating 5?

3. מה עושה df.groupby("author")["pages"].sum()?What does df.groupby("author")["pages"].sum() do?

תרגיליםExercises

תרגילExercise 1

הצצה בנתוניםA first look

קראו את הנתונים לתוך df והדפיסו: מספר השורות, את הכותרת של הספר הארוך ביותר (idxmax), ואת ממוצע הדירוג מעוגל ל-2.

Read the data into df and print: the number of rows, the title of the longest book (idxmax), and the average rating rounded to 2.

איך הפלט אמור להיראות?What should the output look like?
6
Emma
4.33
main.py
רמזHint 1: len(df)len(df)
רמזHint 2: df.loc[df["pages"].idxmax(), "title"]df.loc[df["pages"].idxmax(), "title"]
לפתרוןSolution
תרגילExercise 2

סינוןFiltering

החזירו מ-picks(df) רשימת כותרות של ספרים עם דירוג 4 ומעלה ופחות מ-300 עמודים, ממוינות לפי מספר עמודים מהקצר לארוך.

Return from picks(df) the titles of books rated 4+ with fewer than 300 pages, sorted by pages shortest first.

איך הפלט אמור להיראות?What should the output look like?
['The BFG', 'Matilda', 'Persuasion']
main.py
רמזHint 1: (df["rating"] >= 4) & (df["pages"] < 300)(df["rating"] >= 4) & (df["pages"] < 300)
רמזHint 2: .sort_values("pages")["title"].tolist().sort_values("pages")["title"].tolist()
לפתרוןSolution
תרגילExercise 3

עמודה מחושבתA computed column

הוסיפו ל-df עמודה age (גיל הספר ב-2026) ועמודה size שהיא "short" מתחת ל-250 עמודים, אחרת "long". הדפיסו title, age, size לשלושת הספרים הוותיקים ביותר.

Add to df a column age (the book's age in 2026) and a column size that's "short" under 250 pages, otherwise "long". Print title, age, size for the three oldest books.

איך הפלט אמור להיראות?What should the output look like?
     title  age  size
      Emma  211  long
Persuasion  209 short
The Hobbit   89  long
main.py
רמזHint 1: df["age"] = 2026 - df["year"]df["age"] = 2026 - df["year"]
רמזHint 2: df["pages"].apply(lambda p: "short" if p < 250 else "long")df["pages"].apply(lambda p: "short" if p < 250 else "long")
לפתרוןSolution
תרגילExercise 4

לפי חודשBy month

כתבו per_month(df) שמחזירה מילון: חודש → מספר עמודים שנקראו, בסדר החודשים כפי שהם מופיעים בנתונים (sort=False). השתמשו ב-groupby.

Write per_month(df) returning a dict: month → pages read, in the order months appear in the data (sort=False). Use groupby.

איך הפלט אמור להיראות?What should the output look like?
{'Jan': 412, 'Feb': 714, 'Mar': 518, 'Apr': 249}
main.py
רמזHint 1: df.groupby("month", sort=False)["pages"].sum()df.groupby("month", sort=False)["pages"].sum()
רמזHint 2: .to_dict() הופך Series למילון..to_dict() turns a Series into a dict.
לפתרוןSolution
תרגילExercise 5

מדף, צעד 24: סטטיסטיקת קריאהShelf, step 24: reading statistics

פרויקט מלווהCompanion project

כתבו stats(df) שמחזירה מילון עם: books (כמות), pages (סך עמודים), top_author (הסופר עם הכי הרבה עמודים), best_month (החודש עם הכי הרבה עמודים), ו-avg_rating (מעוגל ל-1).

Write stats(df) returning a dict with: books (count), pages (total pages), top_author (the author with the most pages), best_month (the month with the most pages), and avg_rating (rounded to 1).

איך הפלט אמור להיראות?What should the output look like?
books: 6
pages: 1893
top_author: Jane Austen
best_month: Feb
avg_rating: 4.3
stats.py
רמזHint 1: df.groupby("author")["pages"].sum().idxmax()df.groupby("author")["pages"].sum().idxmax()
רמזHint 2: המירו ל-int ו-float רגילים לפני ההחזרה.Convert to plain int and float before returning.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

ניקוי אמיתיReal cleaning

כתבו clean(text) שמקבלת CSV מלוכלך (עמודות title, author, pages, rating) ומחזירה DataFrame נקי: כותרות וסופרים בלי רווחים מסביב, סופר חסר → unknown, שורות בלי pages נמחקות, pages כ-int, rating חסר → ממוצע הדירוגים (מעוגל ל-1), וכפילויות לפי title נמחקות (נשאר הראשון).

Write clean(text) taking a messy CSV (columns title, author, pages, rating) and returning a clean DataFrame: titles and authors stripped, missing author → unknown, rows without pages dropped, pages as int, missing rating → the mean rating (rounded to 1), and duplicates by title dropped (keep the first).

איך הפלט אמור להיראות?What should the output look like?
  title        author  pages  rating
   Dune Frank Herbert    412     5.0
   Emma       unknown    474     4.5
Matilda    Roald Dahl    240     4.0
main.py
רמזHint 1: df["title"] = df["title"].str.strip() לפני drop_duplicates.df["title"] = df["title"].str.strip() before drop_duplicates.
רמזHint 2: drop_duplicates(subset="title")drop_duplicates(subset="title")
לפתרוןSolution
אתגרChallenge 2

טבלת צירA pivot table

כתבו pivot(df) שמחזירה טבלה שבה כל שורה היא סופר, כל עמודה היא חודש (בסדר Jan, Feb, Mar, Apr), והערכים הם סכום עמודים (0 כשאין). השתמשו ב-pivot_table עם fill_value=0.

Write pivot(df) returning a table where each row is an author, each column a month (in order Jan, Feb, Mar, Apr), and values are page totals (0 where none). Use pivot_table with fill_value=0.

איך הפלט אמור להיראות?What should the output look like?
month             Jan  Feb  Mar  Apr
author                              
Frank Herbert     412    0    0    0
J. R. R. Tolkien    0    0  310    0
Jane Austen         0  474    0  249
Roald Dahl          0  240  208    0
main.py
רמזHint 1: df.pivot_table(index="author", columns="month", values="pages", aggfunc="sum", fill_value=0)df.pivot_table(index="author", columns="month", values="pages", aggfunc="sum", fill_value=0)
רמזHint 2: סדר עמודות: t[["Jan", "Feb", "Mar", "Apr"]]Column order: t[["Jan", "Feb", "Mar", "Apr"]]
לפתרוןSolution

נבנה על ידיBuilt by Ariel Yohimovich