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

חזרה למפגשBack
PythonPython

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

מפגש 24: DataFrame, סינון, קיבוץ וסטטיסטיקה.Session 24: DataFrame, filtering, grouping and statistics.

מה נעשה היוםToday's plan

  • DataFrameDataFrame
  • בחירהSelection
  • סינוןFiltering
  • חישוביםCalculations
  • groupbygroupby
  • ניקויCleaning
נושאTopic 1

DataFrameDataFrame

DataFrame ראשוןYour first DataFrame

  • import pandas as pdimport pandas as pd
  • pd.read_csv → DataFramepd.read_csv → a DataFrame
  • head, shape, info, describehead, shape, info, describe

אנלוגיהAnalogy

DataFrame הוא כמו גיליון אקסל שאפשר לדבר איתו בקוד: במקום לגרור עכבר על 10,000 שורות, כותבים משפט אחד.A DataFrame is like an Excel sheet you can talk to in code: instead of dragging a mouse over 10,000 rows, you write one sentence.

טעות נפוצהCommon mistake

first_df.py
import pandsa as pd
מה Python עונהWhat Python says
ModuleNotFoundError: No module named 'pandsa'
ModuleNotFoundError: No module named 'pandsa': כאן זו שגיאת כתיב. אם השם נכון ועדיין נכשל: pip install pandas בתוך ה-venv.ModuleNotFoundError: No module named 'pandsa': here it's a typo. If the name is right and it still fails: pip install pandas inside the venv.
נושאTopic 2

בחירהSelection

לבחור עמודות ושורותSelecting columns and rows

  • df["title"] → Seriesdf["title"] → a Series
  • df[["title", "pages"]] → טבלה קטנהdf[["title", "pages"]] → a smaller table
  • df.loc[שורה, עמודה]df.loc[row, column]

אנלוגיהAnalogy

עמודה ב-DataFrame היא כמו עמודה בגיליון: לוחצים על הכותרת ומקבלים את כולה, ואז אפשר לבקש סכום או ממוצע בבת אחת.A DataFrame column is like a spreadsheet column: click the header and get all of it, then ask for a sum or average in one go.
נושאTopic 3

סינוןFiltering

סינון ומיוןFiltering and sorting

  • df[df["pages"] > 300]: מסכהdf[df["pages"] > 300]: a mask
  • & ו-| עם סוגריים& and | with parentheses
  • sort_values("pages", ascending=False)sort_values("pages", ascending=False)

אנלוגיהAnalogy

מסכה בוליאנית היא כמו שבלונה שמניחים על הטבלה: רק השורות שמתחת לחורים נראות.A boolean mask is like a stencil laid over the table: only the rows under the holes show through.
נושאTopic 4

חישוביםCalculations

עמודות חדשותNew columns

  • df["hours"] = df["pages"] / 40df["hours"] = df["pages"] / 40
  • פעולה על עמודה שלמה בבת אחתOne operation on a whole column at once
  • .str לפעולות מחרוזת, .apply לפונקציה שלכם.str for string operations, .apply for your own function

אנלוגיהAnalogy

עמודה מחושבת היא כמו נוסחה שגוררים לאורך כל העמודה באקסל: כותבים פעם אחת, וכל שורה מקבלת את התוצאה שלה.A computed column is like a formula dragged down a whole Excel column: write it once, and every row gets its own result.
נושאTopic 5

groupbygroupby

groupby: סיכום לפי קבוצותgroupby: summary by group

  • df.groupby("author")["pages"].sum()df.groupby("author")["pages"].sum()
  • agg לכמה סיכומיםagg for several summaries
  • value_counts() סופר ערכיםvalue_counts() counts values

אנלוגיהAnalogy

groupby הוא כמו למיין חשבוניות לתיקיות לפי ספק, ואז לרשום על כל תיקייה את הסכום הכולל.groupby is like sorting receipts into folders by supplier, then writing each folder's total on the front.
נושאTopic 6

ניקויCleaning

ניקוי וייצואCleaning and exporting

  • NaN = ערך חסרNaN = a missing value
  • isna().sum(), fillna, dropnaisna().sum(), fillna, dropna
  • to_csv(..., index=False)to_csv(..., index=False)

אנלוגיהAnalogy

לנקות נתונים זה כמו לשטוף ירקות לפני בישול: לא מרגש, אבל אם מדלגים, כל המנה יוצאת לא טובה.Cleaning data is like washing vegetables before cooking: not exciting, but skip it and the whole dish suffers.

import pandas as pdimport pandas as pd

pd.read_csv → DataFramepd.read_csv → a DataFrame

head, shape, info, describehead, shape, info, describe

df["title"] → Seriesdf["title"] → a Series

df[["title", "pages"]] → טבלה קטנהdf[["title", "pages"]] → a smaller table

df.loc[שורה, עמודה]df.loc[row, column]

df[df["pages"] > 300]: מסכהdf[df["pages"] > 300]: a mask

& ו-| עם סוגריים& and | with parentheses

sort_values("pages", ascending=False)sort_values("pages", ascending=False)

df["hours"] = df["pages"] / 40df["hours"] = df["pages"] / 40

פעולה על עמודה שלמה בבת אחתOne operation on a whole column at once

.str לפעולות מחרוזת, .apply לפונקציה שלכם.str for string operations, .apply for your own function

df.groupby("author")["pages"].sum()df.groupby("author")["pages"].sum()

agg לכמה סיכומיםagg for several summaries

value_counts() סופר ערכיםvalue_counts() counts values

NaN = ערך חסרNaN = a missing value

isna().sum(), fillna, dropnaisna().sum(), fillna, dropna

מה למדנוWhat we learned

  • import pandas as pdimport pandas as pd
  • df["title"] → Seriesdf["title"] → a Series
  • df[df["pages"] > 300]: מסכהdf[df["pages"] > 300]: a mask
  • df["hours"] = df["pages"] / 40df["hours"] = df["pages"] / 40
  • df.groupby("author")["pages"].sum()df.groupby("author")["pages"].sum()
  • NaN = ערך חסרNaN = a missing value

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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