pandas essentials
Loading, selecting, cleaning and aggregating. 15 cards. Click a card to flip, or use the arrow keys and space bar.
How do you stop 07030 becoming 7030 when reading a CSV?
Read the column as text: pd.read_csv(f, dtype={'zip': str}).
What does dtype object on a date column mean?
It is stored as text, not a date. Convert with pd.to_datetime.
Why use & instead of and when filtering?
pandas needs element-wise operators. Using and raises "truth value of a Series is ambiguous".
Why parenthesise each filter condition?
& binds tighter than comparison operators, so (a > 1) & (b < 5) is required.
loc versus iloc?
loc is label-based and slice-inclusive; iloc is position-based and slice-exclusive.
What does mean() do with missing values?
Skips them, dividing by the non-null count rather than the row count.
What does df.dropna() with no arguments remove?
Every row containing at least one NaN — often most of a wide table.
agg versus transform after groupby?
agg collapses to one row per group; transform returns a value for every original row.
Why call reset_index() after groupby?
The grouping key sits in the index otherwise, which complicates plotting, merging and export.
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.
What does melt do?
Unpivots wide data into long form — turns month columns into month rows.
Why does SettingWithCopyWarning appear?
You assigned into a filtered slice. Add .copy() after filtering, or assign via df.loc.
Why avoid looping over DataFrame rows?
Vectorised column operations are far faster and clearer.
Why pass index=False to to_csv?
Otherwise the index is written as an unnamed extra column.
Why call savefig before show?
show clears the figure, so saving afterwards produces a blank file.