Created
July 18, 2022 21:55
-
-
Save mikekeith52/6a56be9136725028b7d350a87686a3b0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| df2 = df.copy() | |
| df2['year'] = df2.reset_index().DATE.dt.year | |
| df2['month'] = df2.reset_index().DATE.dt.month | |
| df2['pct_chg'] = df2['HOUSTNSA'].pct_change() | |
| avg_chg_month = df2.groupby('month')['pct_chg'].mean().reset_index() | |
| avg_chg_month['pct_chg_std'] = df2.groupby('month')['pct_chg'].std().values | |
| avg_chg_month['pct_chg_2016'] = df2.loc[df2['year'] == 2016,'pct_chg'].values | |
| avg_chg_month['pct_chg_2020'] = df2.loc[df2['year'] == 2020,'pct_chg'].values | |
| avg_chg_month['pct_chg_2021'] = df2.loc[df2['year'] == 2021,'pct_chg'].values | |
| def highlight_rows(row): | |
| lightgreen = 'background-color: lightgreen;' | |
| highlight = 'background-color: lightcoral;' | |
| default = '' | |
| if row['month'] == 3: | |
| return [default,lightgreen,lightgreen,default,default,highlight] | |
| elif row['month'] == 4: | |
| return [default,lightgreen,lightgreen,default,highlight,default] | |
| elif row['month'] == 10: | |
| return [default,lightgreen,lightgreen,highlight,default,default] | |
| else: | |
| return [default,lightgreen,lightgreen,default,default,default] | |
| avg_chg_month.style.apply( | |
| highlight_rows, | |
| axis=1 | |
| ).format( | |
| {var:'{:.1%}'.format for var in avg_chg_month.iloc[:,1:]} | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment