Last active
February 14, 2020 14:24
-
-
Save sauravmishra1710/e91a7fa4b034ad8a26ddce272780c245 to your computer and use it in GitHub Desktop.
Notebook and Pandas Styling - https://pandas.pydata.org/pandas-docs/version/0.18/style.html
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
###################################################################################################### | |
Display markdown like formtted text in notebook via code | |
###################################################################################################### | |
from IPython.display import Markdown | |
'''Display markdown formatted output like bold, italic bold etc.''' | |
def formatted_text(string): | |
display(Markdown(string)) | |
###################################################################################################### | |
highlight the maximum in a Series or DataFrame | |
###################################################################################################### | |
def highlight_max(data, color='yellow'): | |
''' | |
highlight the maximum in a Series or DataFrame | |
''' | |
attr = 'background-color: {}'.format(color) | |
if data.ndim == 1: # Series from .apply(axis=0) or axis=1 | |
is_max = data == data.max() | |
return [attr if v else '' for v in is_max] | |
else: # from .apply(axis=None) | |
is_max = data == data.max().max() | |
return pd.DataFrame(np.where(is_max, attr, ''), | |
index=data.index, columns=data.columns) | |
is_max = data == data.max().max() | |
return pd.DataFrame(np.where(is_max, attr, ''), | |
index=data.index, columns=data.columns) | |
###################################################################################################### | |
create "heatmaps" with the background_gradient method. These require matplotlib, | |
and we'll use Seaborn to get a nice colormap. | |
###################################################################################################### | |
import seaborn as sns | |
cm = sns.light_palette("green", as_cmap=True) | |
s = df.style.background_gradient(cmap=cm) | |
s | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Py Utility functions to enable creation of Markdown like cells using code