Skip to content

Instantly share code, notes, and snippets.

@richiefrost
Created September 3, 2019 12:51
Show Gist options
  • Save richiefrost/12c3ed2f8e8eb26a96b19771db9d6c69 to your computer and use it in GitHub Desktop.
Save richiefrost/12c3ed2f8e8eb26a96b19771db9d6c69 to your computer and use it in GitHub Desktop.
Using pipe() for Pandas DataFrames
def remove_null_cols(df):
_df = df.copy()
_df = df.dropna(how='all', axis=1)
return _df
def set_category_types(df, columns):
_df = df.copy()
for col in columns:
_df[col] = df[col].astype('category')
return _df
def convert_to_datetime(df, columns):
_df = df.copy()
for col in columns:
_df[col] = pd.to_datetime(_df[col])
return _df
# However you get your dataframe, get it here
df = get_fake_cars_dataframe()
# Apply some sequential transformations to our car
df = df.pipe(remove_null_cols) \
.pipe(set_category_types(df, ['Make', 'Model', 'Color']) \
.pipe(convert_to_datetime, ['CreatedDate', 'BoughtDate', 'SoldDate'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment