Skip to content

Instantly share code, notes, and snippets.

@adiamaan92
Created January 27, 2019 18:00
Show Gist options
  • Save adiamaan92/ed8fd08aa5e827575c410b197825369e to your computer and use it in GitHub Desktop.
Save adiamaan92/ed8fd08aa5e827575c410b197825369e to your computer and use it in GitHub Desktop.
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":
return df.iloc[:, fn(df)]
(
iris.pipe(
setcols,
fn=lambda x: x.columns.str.lower()
.str.replace(r"\(cm\)", "")
.str.strip()
.str.replace(" ", "_"),
).pipe(cfilter, lambda x: x.columns.str.contains("sepal"), axis="columns")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment