How do I get the row count of a Pandas DataFrame?

For a dataframe df, one can use any of the following: len(df.index) df.shape[0] df[df.columns[0]].count() (== number of non-NaN values in first column) Code to reproduce the plot: import numpy as np import pandas as pd import perfplot perfplot.save( “out.png”, setup=lambda n: pd.DataFrame(np.arange(n * 3).reshape(n, 3)), n_range=[2**k for k in range(25)], kernels=[ lambda df: len(df.index), lambda … Read more

fatal error: Python.h: No such file or directory

Looks like you haven’t properly installed the header files and static libraries for python dev. Use your package manager to install them system-wide. For apt (Ubuntu, Debian…): sudo apt-get install python-dev # for python2.x installs sudo apt-get install python3-dev # for python3.x installs For yum (CentOS, RHEL…): sudo yum install python-devel # for python2.x installs … Read more

Random string generation with upper case letters and digits

Answer in one line: ”.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N)) or even shorter starting with Python 3.6 using random.choices(): ”.join(random.choices(string.ascii_uppercase + string.digits, k=N)) A cryptographically more secure version: see this post ”.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N)) In details, with a clean function for further reuse: >>> import string >>> import random >>> … Read more

Proper way to declare custom exceptions in modern Python?

Maybe I missed the question, but why not: class MyException(Exception): pass To override something (or pass extra args), do this: class ValidationError(Exception): def __init__(self, message, errors): # Call the base class constructor with the parameters it needs super().__init__(message) # Now for your custom code… self.errors = errors That way you could pass dict of error … Read more

How to print to stderr in Python?

I found this to be the only one short, flexible, portable and readable: # This line only if you still care about Python2 from __future__ import print_function import sys def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) The optional function eprint saves some repetition. It can be used in the same way as the standard print function: … Read more

What is the meaning of single and double underscore before an object name?

Single Underscore In a class, names with a leading underscore indicate to other programmers that the attribute or method is intended to be be used inside that class. However, privacy is not enforced in any way. Using leading underscores for functions in a module indicates it should not be imported from somewhere else. From the … Read more

How do I install pip on macOS or OS X?

TLDR. On any modern Mac python3 -m ensurepip then pip3 –version to check. pip’s documentation lists the supported mechanisms to install it: https://pip.pypa.io/en/stable/installation/#supported-methods It is generally recommended to avoid installing pip on the OS-provided python commands, and to install Python via the official installers or using something like Homebrew or pyenv. Python 3.4+ will have … Read more

How to prettyprint a JSON file?

The json module already implements some basic pretty printing in the dump and dumps functions, with the indent parameter that specifies how many spaces to indent by: >>> import json >>> >>> your_json = ‘[“foo”, {“bar”:[“baz”, null, 1.0, 2]}]’ >>> parsed = json.loads(your_json) >>> print(json.dumps(parsed, indent=4, sort_keys=True)) [ “foo”, { “bar”: [ “baz”, null, 1.0, … Read more