When retrieving a slice with loc in Python Pandas, the end point is included, highly unusual in Python!
Reading a CSV file into a Python Pandas data frame? Speed things up by specifying the PyArrow engine. Data storage isn’t affected. With a 2.2GB file, it took 4s vs. 55s — more than 10x faster!
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!