Last active
July 5, 2022 19:27
-
-
Save JoshZastrow/1c16a0214e6f904af77d55182d60ebda to your computer and use it in GitHub Desktop.
Pandas Tips
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
import pandas as pd | |
def filter_columns_by_keyword(df: pd.DataFrame, keyword: str)-> pd.DataFrame: | |
"""Filters pandas columns by a keyword by searching the column index for a matching substring""" | |
return df.loc[:, lambda d: d.columns.str.contains(keyword)] | |
def convert_monthly_ts(df): | |
return df.reset_index().assign(ds_month = lambda x: x.ds + pd.offsets.MonthBegin()).groupby(["job_area", "ds_month"], as_index=False).y.mean() | |
source = pd.merge( | |
df_forecast.pipe(convert_monthly_ts), | |
df_es.pipe(convert_monthly_ts), | |
on=["job_area", "ds_month"], | |
suffixes=("_rolling", "_expanding"), | |
) | |
# convert two number columns to a single column with an identifier column for visualization | |
source = source.melt(id_vars=["job_area", "ds_month"], value_vars=["y_rolling", "y_expanding"], var_name="statistic", value_name="y") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment