Created
September 3, 2019 12:51
-
-
Save richiefrost/12c3ed2f8e8eb26a96b19771db9d6c69 to your computer and use it in GitHub Desktop.
Using pipe() for Pandas DataFrames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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