ModuleNotFoundError: No module named ‘OpenSSL’

The OpenSSL module comes from the pyOpenSSL library. You can install it with Pip using a command like: pip install pyOpenSSL If it fails due to missing dependencies, see the instructions on installing pyOpenSSL’s dependencies in this answer to “How to install OpenSSL for Python”. (Also, as with all pip installs, depending upon your environment, … Read more

Shapely point geometry in geopandas df to lat/lon columns

If you have the latest version of geopandas (0.3.0 as of writing), and the if df is a GeoDataFrame, you can use the x and y attributes on the geometry column: df[‘lon’] = df.point_object.x df[‘lat’] = df.point_object.y In general, if you have a column of shapely objects, you can also use apply to do what … Read more

Get output from a Paramiko SSH exec_command continuously

A minimal and complete working example of how to use this answer (tested in Python 3.6.1) # run.py from paramiko import SSHClient ssh = SSHClient() ssh.load_system_host_keys() ssh.connect(‘…’) print(‘started…’) stdin, stdout, stderr = ssh.exec_command(‘python -m example’, get_pty=True) for line in iter(stdout.readline, “”): print(line, end=””) print(‘finished.’) and # example.py, at the server import time for x in … Read more

How to check if a pandas dataframe contains only numeric values column-wise?

You can check that using to_numeric and coercing errors: pd.to_numeric(df[‘column’], errors=”coerce”).notnull().all() For all columns, you can iterate through columns or just use apply df.apply(lambda s: pd.to_numeric(s, errors=”coerce”).notnull().all()) E.g. df = pd.DataFrame({‘col’ : [1,2, 10, np.nan, ‘a’], ‘col2’: [‘a’, 10, 30, 40 ,50], ‘col3’: [1,2,3,4,5.0]}) Outputs col False col2 False col3 True dtype: bool

Create a slice using a tuple

You can use Python’s *args syntax for this: >>> a = range(20) >>> b = (5, 12) >>> a[slice(*b)] [5, 6, 7, 8, 9, 10, 11] Basically, you’re telling Python to unpack the tuple b into individual elements and pass each of those elements to the slice() function as individual arguments.

Plotting Pandas Multiindex Bar Chart

import pandas as pd data = pd.DataFrame([ (‘Q1′,’Blue’,100), (‘Q1′,’Green’,300), (‘Q2′,’Blue’,200), (‘Q2′,’Green’,350), (‘Q3′,’Blue’,300), (‘Q3′,’Green’,400), (‘Q4′,’Blue’,400), (‘Q4′,’Green’,450), ], columns=[‘quarter’, ‘company’, ‘value’] ) data = data.set_index([‘quarter’, ‘company’]).value data.unstack().plot(kind=’bar’, stacked=True) If you don’t want to stack your bar chart: data.unstack().plot(kind=’bar’)

File not found.