pbPassingBI
/

pandas essentials

Loading, selecting, cleaning and aggregating. 15 cards. Click a card to flip, or use the arrow keys and space bar.

  1. How do you stop 07030 becoming 7030 when reading a CSV?

    Read the column as text: pd.read_csv(f, dtype={'zip': str}).

  2. What does dtype object on a date column mean?

    It is stored as text, not a date. Convert with pd.to_datetime.

  3. Why use & instead of and when filtering?

    pandas needs element-wise operators. Using and raises "truth value of a Series is ambiguous".

  4. Why parenthesise each filter condition?

    & binds tighter than comparison operators, so (a > 1) & (b < 5) is required.

  5. loc versus iloc?

    loc is label-based and slice-inclusive; iloc is position-based and slice-exclusive.

  6. What does mean() do with missing values?

    Skips them, dividing by the non-null count rather than the row count.

  7. What does df.dropna() with no arguments remove?

    Every row containing at least one NaN — often most of a wide table.

  8. agg versus transform after groupby?

    agg collapses to one row per group; transform returns a value for every original row.

  9. Why call reset_index() after groupby?

    The grouping key sits in the index otherwise, which complicates plotting, merging and export.

  10. How do you catch a merge that duplicated rows?

    Compare len() before and after, or pass validate="many_to_one" to raise on a bad relationship.

  11. What does melt do?

    Unpivots wide data into long form — turns month columns into month rows.

  12. Why does SettingWithCopyWarning appear?

    You assigned into a filtered slice. Add .copy() after filtering, or assign via df.loc.

  13. Why avoid looping over DataFrame rows?

    Vectorised column operations are far faster and clearer.

  14. Why pass index=False to to_csv?

    Otherwise the index is written as an unnamed extra column.

  15. Why call savefig before show?

    show clears the figure, so saving afterwards produces a blank file.