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”:
Create a Python dict with keys a/b/c and values 0: But don’t do this, since the value will be shared and mutable: {‘a’: [‘x’, ‘y’], ‘b’: [‘x’, ‘y’], ‘c’: [‘x’, ‘y’]} 🤯
“round” rounds Python numbers: Remember, .5 goes to the nearest *even* value: