Writing a Python module? Want to control which names are exported via “from .. import *”? Define __all__, a list of strings indicating which names will be exported via “import *”:
Using many names from a Python module? Import them all: Wait: This is usually a TERRIBLE idea! • Potential namespace collisions• Less readable code• Module upgrades affect globals in your program I know, it’s tempting — but don’t!
Python trick: 0 is falsy, 1 is truthy. Use this for even/odd checks: No need for “if n % 2 == 0” — just use the truthiness!
Calling astype on Python Pandas series with unconvertible values? Use errors=’ignore’ to skip the error:
Python Pandas astype() returns a NEW series — it doesn’t modify in place: Watch out: You’ll get a TypeError if any value can’t be converted
How many NaNs in a Python Pandas data frame? 1. Use isna, returning a data frame of booleans2. Use sum, returning the number of NaNs in each column3. Use sum again, adding those numbers
How many NaNs in a Python Pandas series? Use isna (returns True/False) then sum/value_counts: Hint: Pass normalize=True for the percentage
Remember that Python has two division operators:10 / 2 # truediv, returns float: 5.010 // 2 # floordiv, rounds down: 510 / 3 # truediv: 3.333…10 // 3 # floordiv: 3 Note: // with floats returns a float (10.0 // 3 = 3.0)
Reading CSV into Python Pandas? Pass header to specify the row containing column names: No headers at all? Just pass headers=None: Pass a list for multi-indexed column names:
Python Pandas: pivot vs pivot_table? Both reshape data, but: – pivot: one value per cell (errors if duplicates)– pivot_table: aggregates duplicates (mean, sum, etc.) Use pivot for clean data, pivot_table for real-world messiness.