Skip to content

Instantly share code, notes, and snippets.

View adiamaan92's full-sized avatar
🎯
Focusing

Adiamaan Keerthi adiamaan92

🎯
Focusing
View GitHub Profile
def cfilter(df, fn, axis="rows"):
""" Custom Filters based on a condition and returns the df.
function - a lambda function that returns a binary vector
thats similar in shape to the dataframe
axis = rows or columns to be filtered.
A single level indexing
"""
if axis == "rows":
return df[fn(df)]
elif axis == "columns":
import logging
import sys
logger = logging.getLogger()
fhandler = logging.FileHandler(filename="mylog.log", mode="a")
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
fhandler.setFormatter(formatter)
logger.addHandler(fhandler)
data = load_iris()
iris = pd.DataFrame(data.data, columns=data.feature_names)
(
iris.pipe(csnap, lambda x: x.head(), msg="Before")
.pipe(
setcols,
fn=lambda x: x.columns.str.lower()
.str.replace(r"\(cm\)", "")
.str.strip()
(
wine.pipe(csnap)
.rename(columns={"color_intensity": "ci"})
.assign(color_filter=lambda x: np.where((x.hue > 1) & (x.ci > 7), 1, 0))
.pipe(csnap)
.query("alcohol > 14")
.pipe(csnap)
.sort_values("alcohol", ascending=False)
.reset_index(drop=True)
.loc[:, ["alcohol", "ci", "hue"]]
def csnap(df, fn=lambda x: x.shape, msg=None):
""" Custom Help function to print things in method chaining.
Returns back the df to further use in chaining.
"""
if msg:
print(msg)
display(fn(df))
return df
@adiamaan92
adiamaan92 / wine_mc_example.py
Created January 27, 2019 17:05
wine_mc_example
(
wine.rename(columns={"color_intensity": "ci"})
.assign(color_filter=lambda x: np.where((x.hue > 1) & (x.ci > 7), 1, 0))
.query("alcohol > 14 and color_filter == 1")
.sort_values("alcohol", ascending=False)
.reset_index(drop=True)
.loc[:, ["alcohol", "ci", "hue"]]
)
@adiamaan92
adiamaan92 / wine_load.py
Last active January 27, 2019 16:57
wine_load
data = load_wine()
wine = pd.DataFrame(data.data,
columns=data.feature_names)
wine.head()
@adiamaan92
adiamaan92 / jack_jill_method_chaining
Created January 27, 2019 16:37
jack_jill_method_chaining
(
jack_jill
.went_up("hill")
.fetch("water")
.fell_down("jack")
.broke("crown")
.tumble_after("jill")
)
@adiamaan92
adiamaan92 / jack_jill_nested_calls
Last active January 27, 2019 16:37
jack_jill_nested_calls
tumble_after(
broke(
fell_down(
fetch(went_up(jack_jill, "hill"), "water"),
jack),
"crown"),
"jill"
)
@adiamaan92
adiamaan92 / .block
Last active August 24, 2016 03:56
Phone Connection in Service Areas - India
license: mit