Style source
State: closed

I am attempting to style a column based on another column's data, for example:

``` python
import pandas as pd

now = pd.datetime.now()
rng = now + pd.timedelta_range("1D","10D",freq='D')

df = pd.DataFrame(rng,columns=["ExactDate"])

df['Display Date'] = df['ExactDate'].dt.strftime("%m-%Y")

style = df.style.bar(subset='Display Date', source = rng)

html = style.render()

print html
```

Gives: 
![image](https://cloud.githubusercontent.com/assets/9845637/11811135/7a8fd588-a32b-11e5-8d0b-03c73c417040.png)


Labels:
name: Output-Formatting
description: __repr__ of pandas objects, to_string

Comments:
Thread about this: #11610

I'd rather not complicate the signature of `.bar` (and presumably every style function, why limit this to just bar?) with a `source` argument.

I'm strapped for time right now so I can't write up an example, but what you want can be accomplished through `Styler.apply`. I'll check back in in a couple weeks.

@TomAugspurger , sorry I should have answered before, I took your advice and wrote a custom function to perform this, and used the apply method. it works great, I just needed a push in the right direction, thanks. The feature itself is very useful, kudos.

I agree that my suggested change would convolute the code and would indeed need to be applied to all styling functions. One day I'll make a useful and worthy PR!

for others that may come across this and want an example:

``` python
def _table_bar(s, colour, vmin = 0 , vmax = 100, c_source = None):
        if c_source is not None:
            s = c_source

        normed = vmin +((vmax-vmin) * (s - s.min()) / (s.max() - s.min()))
        attrs = 'width: 10em; height: 80%;'\
            'background: linear-gradient(90deg,'\
            '{c} {w}%, transparent 0%)'
        return [attrs.format(c=colour, w=x) for x in normed]
```

is as `Styler.bar` but with a c_source argument, called by:

``` python
df.style.apply(_table_bar, colour="#D55E00", subset='Display Date',  c_source = df['ExactDate'].values)
```

achieving the above desired:

![image](https://cloud.githubusercontent.com/assets/9845637/11811135/7a8fd588-a32b-11e5-8d0b-03c73c417040.png)

Glad to hear you worked it out!

We might be able to refactor the color handling code and include that as part of the public API.

-Tom

> On Dec 16, 2015, at 12:02 PM, joekane3 notifications@github.com wrote:
> 
> for others that may come across this and want an example:
> 
> def _table_bar(s, colour, vmin = 0 , vmax = 100, c_source = None):
>         if c_source is not None:
>             s = c_source
> 
> ```
>     normed = vmin +((vmax-vmin) * (s - s.min()) / (s.max() - s.min()))
>     attrs = 'width: 10em; height: 80%;'\
>         'background: linear-gradient(90deg,'\
>         '{c} {w}%, transparent 0%)'
>     return [attrs.format(c=colour, w=x) for x in normed]
> ```
> 
> is as Styler.bar but with a c_source argument, called by:
> 
> df.style.apply(_table_bar, colour="#D55E00", subset='Display Date',  c_source = df['ExactDate'].values)
> achieving the above desired:
> 
>  https://cloud.githubusercontent.com/assets/9845637/11811135/7a8fd588-a32b-11e5-8d0b-03c73c417040.png
> —
> Reply to this email directly or view it on GitHub https://github.com/pydata/pandas/pull/11844#issuecomment-165191982.