נבנה על ידיBuilt by Ariel Yohimovich
import pandas as pdimport pandas as pdpd.read_csv → DataFramepd.read_csv → a DataFramehead, shape, info, describehead, shape, info, describepd.read_csv קורא קובץ לDataFrame, טבלה עם עמודות בשמות. במקום לולאות, כותבים פעולה אחת על עמודה שלמה. head() מראה את השורות הראשונות ו-shape את הגודל. (כאן הנתונים בתוך מחרוזת כדי שהדוגמה תרוץ בכל מקום; אצלכם: pd.read_csv("books.csv").)pandas is the library that made Python the world's data language. pd.read_csv reads a file into a DataFrame, a table with named columns. Instead of loops, you write one operation on a whole column. head() shows the first rows and shape the size. (Here the data lives in a string so the example runs anywhere; on your machine: pd.read_csv("books.csv").)import pandsa as pdModuleNotFoundError: 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.df["title"] → Seriesdf["title"] → a Seriesdf[["title", "pages"]] → טבלה קטנהdf[["title", "pages"]] → a smaller tabledf.loc[שורה, עמודה]df.loc[row, column]df["pages"]. כמה עמודות, ברשימה בתוך הסוגריים: df[["title", "pages"]] (שימו לב לסוגריים הכפולים). לשורות לפי מספר iloc, ולפי תווית loc. ועל Series אפשר לחשב מיד: .mean(), .max(), .sum().One column is a Series: df["pages"]. Several columns go in a list inside the brackets: df[["title", "pages"]] (note the double brackets). For rows by position use iloc, by label loc. And you can compute on a Series right away: .mean(), .max(), .sum().df[df["pages"] > 300]: מסכהdf[df["pages"] > 300]: a mask& ו-| עם סוגריים& and | with parenthesessort_values("pages", ascending=False)sort_values("pages", ascending=False)df["pages"] > 300 לא מחזיר True אחד, אלא Series של True/False לכל שורה: מסכה בוליאנית. כשמכניסים אותה לסוגריים, נשארות רק השורות עם True. לכמה תנאים משתמשים ב-& (וגם) ו-| (או), וכל תנאי בסוגריים משלו. sort_values ממיין.df["pages"] > 300 doesn't return one True, but a Series of True/False per row: a boolean mask. Put it inside the brackets and only the True rows remain. For several conditions use & (and) and | (or), each condition in its own parentheses. sort_values sorts.df["hours"] = df["pages"] / 40df["hours"] = df["pages"] / 40.str לפעולות מחרוזת, .apply לפונקציה שלכם.str for string operations, .apply for your own function.str: df["author"].str.upper(). ולכל דבר אחר, .apply(פונקציה) מריץ פונקציה שלכם על כל ערך.Create a new column by assignment, and the calculation runs on every row without a loop. For text there's .str: df["author"].str.upper(). For anything else, .apply(function) runs your function on each value.df.groupby("author")["pages"].sum()df.groupby("author")["pages"].sum()agg לכמה סיכומיםagg for several summariesvalue_counts() סופר ערכיםvalue_counts() counts valuesagg מאפשר כמה סיכומים בבת אחת. ו-value_counts() הוא הדרך המהירה לספור כמה פעמים מופיע כל ערך. זה בדיוק מה שנצטרך לגרף הקריאה החודשי במפגש הבא.groupby splits the table into groups (e.g. by author) and summarises each: sum, mean, count. agg gives several summaries at once. And value_counts() is the quick way to count how often each value appears. That's exactly what we'll need for next session's monthly reading chart.isna().sum(), fillna, dropnaisna().sum(), fillna, dropnato_csv(..., index=False)to_csv(..., index=False)isna().sum() סופר חסרים בכל עמודה, fillna ממלא ו-dropna מוחק שורות. drop_duplicates מסיר כפילויות. בסוף, to_csv שומר את התוצאה הנקייה (index=False כדי לא לשמור את מספרי השורות).Real data is messy: empty cells, spaces, duplicates. pandas marks a missing value as NaN. isna().sum() counts missing per column, fillna fills and dropna drops rows. drop_duplicates removes duplicates. Finally, to_csv saves the clean result (index=False to skip the row numbers).import pandas as pdimport pandas as pdpd.read_csv → DataFramepd.read_csv → a DataFramehead, shape, info, describehead, shape, info, describedf["title"] → Seriesdf["title"] → a Seriesdf[["title", "pages"]] → טבלה קטנהdf[["title", "pages"]] → a smaller tabledf.loc[שורה, עמודה]df.loc[row, column]df[df["pages"] > 300]: מסכהdf[df["pages"] > 300]: a mask& ו-| עם סוגריים& and | with parenthesessort_values("pages", ascending=False)sort_values("pages", ascending=False)df["hours"] = df["pages"] / 40df["hours"] = df["pages"] / 40.str לפעולות מחרוזת, .apply לפונקציה שלכם.str for string operations, .apply for your own functiondf.groupby("author")["pages"].sum()df.groupby("author")["pages"].sum()agg לכמה סיכומיםagg for several summariesvalue_counts() סופר ערכיםvalue_counts() counts valuesisna().sum(), fillna, dropnaisna().sum(), fillna, dropnaimport pandas as pdimport pandas as pddf["title"] → Seriesdf["title"] → a Seriesdf[df["pages"] > 300]: מסכהdf[df["pages"] > 300]: a maskdf["hours"] = df["pages"] / 40df["hours"] = df["pages"] / 40df.groupby("author")["pages"].sum()df.groupby("author")["pages"].sum()נבנה על ידיBuilt by Ariel Yohimovich