Q:

How to format numbers based on row condition in Pandas to_html

I have data frame with columns country measure and values :
Country| Measures| Value|
US    |Sales    |10000.000
US    |Share %  |0.10265
US    |Price    |15.82564
UK    |Sales    |10500.000
UK    |Share %  |0.30265
UK    |Price    |13.72564

I load dataframe from database which has floating point number upto 6 digits,
I want below fomat in html 
Country| Measures |Values
US    |Sales    |10000
US    |Share %  |10.2%
US    |Price    |15.83
UK    |Sales    |10500
UK    |Share %  |30.27%
UK    |Price    |13.72

Using 'float_format'  or  'formatters '  parameter of df.to_html function I can convert all number into desired format or column specific format respectively 
below code I used in column specific formatting 
where I have Share and price in columns not in rows
df.to_html(index=False, formatters={ 'Share %': '{:,.1%}'.format,
                                                      'Price': '{:,.1f}'.format})

But here my requirement is to format on the basis of rows 
I want 

sales number in Integer
Share number in percentage with 2 decimal
Price number with 2 decimal

Is there any way to achieve this?

A:

You can use string formatting
fmap = dict(
    Sales='{:0.0f}'.format,
    Price='{:0.2f}'.format,
    Share='{:0.2%}'.format
)

df.assign(Value=[fmap[m](v) for m, v in zip(df.Measures, df.Value)])

  Country Measures   Value
0      US    Sales   10000
1      US    Share  10.27%
2      US    Price   15.83
3      UK    Sales   10500
4      UK    Share  30.26%
5      UK    Price   13.73

Alternate dictionary specification
fmap = {
    'Sales': '{:0.0f}'.format,
    'Price': '{:0.2f}'.format,
    'Share': '{:0.2%}'.format
}

