In [1]:
%matplotlib inline
import pandas as pd
import numpy as np
import seaborn as sns
pd.options.display.max_rows=12
pd.options.display.width=80

# read a csv
df = pd.read_csv('~/seaborn-data/iris.csv',index_col=False)
# adjust column names
df.columns = df.columns.str.replace('\s+','_').str.lower()
# in pandas 0.17.1 and more : sample and style ... 
def color_negative_red(val):
    """
    Takes a scalar and returns a string with
    the css property `'color: red'` for negative
    strings, black otherwise.
    """
    color = 'red' if   (isinstance(val, (int,   float)) and val < 3) else 'black'
    return 'color: %s' % color

def highlight_max(s):
    '''
    highlight the maximum in a Series
    '''
    is_max = s == s.max()
    return ['background-color: yellow' if v else '' for v in is_max]

cm = sns.light_palette("green", as_cmap=True)

(df
   .sample (n=7)
   .style
   .applymap(color_negative_red, subset=pd.IndexSlice[['sepal_width', 'petal_width']])
   .bar(subset=['sepal_length', 'petal_length'], color='#7F7FFF')
   .background_gradient(subset=['sepal_width', 'petal_width'], cmap=cm)
   .apply(highlight_max)
   .highlight_null(null_color='red')
)


Out[1]:
sepal_length sepal_width petal_length petal_width species
45 4.8 3.0 1.4 0.3 setosa
125 7.2 3.2 6.0 1.8 virginica
49 5.0 3.3 1.4 0.2 setosa
95 5.7 3.0 4.2 1.2 versicolor
84 5.4 3.0 4.5 1.5 versicolor
16 5.4 3.9 1.3 0.4 setosa
146 6.3 2.5 5.0 1.9 virginica

In [2]:
# assign = define new temporary columns (like 'mutate' in R language)
(df
   .query('sepal_length > 5')
   .assign(sepal_ratio = lambda x: x.sepal_width / x.sepal_length,
           petal_ratio = lambda x: x.petal_width / x.petal_length)
   .plot
   .scatter(x='sepal_ratio', y='petal_ratio', figsize=(8,4))
)


Out[2]:
<matplotlib.axes._subplots.AxesSubplot at 0x48efc88>

In [3]:
# pipe = like '%>%' in R language
(df
   .query('sepal_length > 5')
   .assign(sepal_ratio = lambda x: x.sepal_width / x.sepal_length,
           petal_ratio = lambda x: x.petal_width / x.petal_length)
   .pipe(sns.pairplot, hue='species', size=1.5)
)


Out[3]:
<seaborn.axisgrid.PairGrid at 0xa6336a0>

In [4]:
# Pandas interactive
import pandas as pd
import numpy as np

# create a df with random datas
np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
               axis=1)
df.iloc[0, 2] = np.nan

# interactive
from IPython.html import widgets
@widgets.interact
def f(h_neg=(0, 359, 1), h_pos=(0, 359), s=(0., 99.9), l=(0., 99.9)):
    return (df
             .style
             .background_gradient(
                cmap=sns.palettes.diverging_palette(
                     h_neg=h_neg, h_pos=h_pos, s=s, l=l, as_cmap=True)
             ).highlight_null()
           )


A B C D E
0 1.0 1.329212 nan -0.31628 -0.99081
1 2.0 -1.070816 -1.438713 0.564417 0.295722
2 3.0 -1.626404 0.219565 0.678805 1.889273
3 4.0 0.961538 0.104011 -0.481165 0.850229
4 5.0 1.453425 1.057737 0.165562 0.515018
5 6.0 -1.336936 0.562861 1.392855 -0.063328
6 7.0 0.121668 1.207603 -0.00204 1.627796
7 8.0 0.354493 1.037528 -0.385684 0.519818
8 9.0 1.686583 -1.325963 1.428984 -2.089354
9 10.0 -0.12982 0.631523 -0.586538 0.29072

In [5]:
from IPython.display import HTML

def hover(hover_color="#ff0f99"):
    return dict(selector="tr:hover",
                props=[("background-color", "%s" % hover_color)])

styles = [
    hover(),
    dict(selector="th", props=[("font-size", "150%"),
                               ("text-align", "center")]),
    dict(selector="caption", props=[("caption-side", "bottom")])
]
html = (df.style.set_table_styles(styles)
          .set_caption("Hover to highlight."))
html


Out[5]:
Hover to highlight.
A B C D E
0 1.0 1.329212 nan -0.31628 -0.99081
1 2.0 -1.070816 -1.438713 0.564417 0.295722
2 3.0 -1.626404 0.219565 0.678805 1.889273
3 4.0 0.961538 0.104011 -0.481165 0.850229
4 5.0 1.453425 1.057737 0.165562 0.515018
5 6.0 -1.336936 0.562861 1.392855 -0.063328
6 7.0 0.121668 1.207603 -0.00204 1.627796
7 8.0 0.354493 1.037528 -0.385684 0.519818
8 9.0 1.686583 -1.325963 1.428984 -2.089354
9 10.0 -0.12982 0.631523 -0.586538 0.29072

Beginners Training Video: "Brandon Rhodes - Pandas From The Ground Up - PyCon 2015 "

Pandas API reference


In [ ]: