Does your Python Pandas column contain repeated strings? Turn it into a ‘category’ series (like an enum) to save memory: That’s right — 90% less memory!
Make your Python comprehensions easier to read, write, and debug with multiple lines. # better than:[n**2 for n in range(10) if n%2]
Want to count values in a Python sequence? Use Counter: Bonus: Counter inherits from dict, getting its methods + operators.
Why use OrderedDict if Python dicts are now ordered? Because equality checks order too:
Make a Python class sortable with __lt__ and __eq__:
By default, str.strip in Python removes leading/trailing whitespace: It takes an optional argument, naming leading/trailing characters to remove. Great for cleaning strings:
Instead of choosing which Python function to run with if/elif/else, use a dispatch table — a dict with functions as values:
Modern Python dicts remember insertion order: This applies in for loops, too!
Using Python defaultdict? Don’t pass a value: Pass a function that returns the default:
Your Python function takes a filename and **kwargs. But this raises an error, since “filename” is passed twice: Solution: Set filename to be positional only: Now any keyword argument works!