Which is faster in Python Pandas, | or isin? I prefer isin; it’s clearer to write and read. Plus, fewer worries about parentheses. But is it faster? Depends on the dtype! isin edges out | on strings, but | wins on ints. Bottom line: Use %%timeit to check. Don’t just guess!
Want rows in a Python Pandas data frame that might have several values? Another way (besides the | I showed yesterday) is the “isin” method: We’ll compare isin vs. | speed tomorrow. But I find this far more readable.
Want rows in a Python Pandas data frame that might have several values? You can use the | operator, but be sure to use () to avoid precedence issues:
Stacking loc to filter a Python Pandas dataframe? Order can matter:
Filtering rows in a Python Pandas data frame? Use .loc: pd.col refers to the previous line’s returned data frame. So we can stack them:
How much memory does a Python list use? sys.getsizeof will tell you, sort of: It reports the memory used by the list, but not its elements:
Want stderr in your Python program to go somewhere else? Use redirect_stderr in contextlib:
Writing a bunch to non-stdout in Python? You might want to use redirect_stdout from contextlib:
Want to print to stderr, not stdout, in a Python program? Use sys.stderr and the “file” kwarg: stdout and stderr often look identical in a terminal — but they aren’t!
Normally, “print” in Python writes to stdout. Write elsewhere with the “file” keyword argument: