Sorting a Python dict by value? Instead of: Use itemgetter. It’s cleaner AND ~25% faster: Sort by value, then by key:
Write to a file in Python with open(filename, ‘w’). What if filename already exists? Its contents are gone. (I hope you have backups!) Instead, try: If filename already exists, ‘x’ raises an exception. For new files, ‘x’ and ‘w’ are the same.
Retrieve rows of a Python Pandas data frame with the 6 largest values of column x: Even better: Why is it better? On a 3m row, 680 MB data frame, sort/tail took 622 ms. nlargest? Only 84 ms — 7x faster!
A Python Pandas rule of thumb: Wherever you can use a column name (string), you can use multiple column names (a list of strings):
To sort a Python Pandas data frame by column x: By x and y (ascending): By x and y, both descending: Ascending x, descending y:
Need specific column types in Python Pandas? Use select_dtypes:
Use parentheses to split long Python code across lines: But with parentheses, Python sees it as one line: Or in comprehensions…
Python Jupyter memory quirk: Deleting variables doesn’t always free memory! Use df.head() instead of df to keep big values out of Jupyter’s Out dict.
Playing NYT Spelling Bee? Use Python to see if your word only uses allowed letters (ignoring length + center): With sets, <= checks “subset or equal”: