Python Code Cookbook

Python Code Cookbook

2018-07-01. Category & Tags: Default Python, Reuse, Import, Include

See also:

Pandas #

Get column_nr in pandas/numpy.

if isinstance(X, pd.DataFrame):
    X_is_df = True
    row_nr = X.shape[0]
    column_nr = X.shape[1]
    if isinstance(X.columns, pd.core.index.MultiIndex):
        df_columns_is_multi_index = True
    else:
        df_columns_is_multi_index = False
else: # non-DF
    X_is_df = False
    row_nr = np.shape(X)[0]
    column_nr = np.shape(X)[1]

use dataframe directly against spark #

lib Optimus

from optimus import Optimus
op = Optimus(master="local")
df = op.create.df(<some data>)
...

Sourcing/Importing #

execfile or os.system #

execute the other python file in place:

execfile("/path/to/my/lib/mylib.py")

or:

import os
os.system("python /path/to/my/lib/mylib.py")

import #

import sys
import os
sys.path.append(os.path.abspath("/path/to/my/lib"))
import mylib

ref #

See other methods here.