简体   繁体   中英

Changes in pandas dataframe appearance

I have a data frame, I need to change the look and feel of that dataframe to send that dataframe over email. I need to change the color of individual rows needs to make some rows bold in between the dataframe.

Input DF:-

 Country 2019 2018 2019 2018 Difference Difference% Country/INR Header_1 Header_2 0 Netherlands 3,661 2,875 3,661 2,875 786 30.00% 1 Croatia 396 279 396 279 117 30.00% 2 Belgium 2,247 1,890 2,247 1,890 357 30.00% 3 2019 2018 2019 2018 Difference Difference% 4 Country/INR Header_1 Header_2 5 Netherlands 3,661 2,875 3,661 2,875 786 30.00% 6 Croatia 396 279 396 279 117 30.00% 7 Belgium 2,247 1,890 2,247 1,890 357 30.00% 

在此处输入图片说明

Output needed :-

 Country Wise Data 2019 2018 2019 2018 Difference Difference% Country/INR Header_1 Header_2 Header_3 0 Netherlands 3,661 2,875 3,661 2,875 786 30.00% 1 Croatia 396 279 396 279 117 30.00% 2 Belgium 2,247 1,890 2,247 1,890 357 30.00% 3 2019 2018 2019 2018 Difference Difference% 4 Country/INR Header_3 Header_4 Header_5 5 Netherlands 3,661 2,875 3,661 2,875 786 30.00% 6 Croatia 396 279 396 279 117 30.00% 7 Belgium 2,247 1,890 2,247 1,890 357 30.00% 

在此处输入图片说明

If someone have some HTML changes done with the dataframes. Please share with me. (How to use df.to_html)

Thanks in advance.

I don't understand the structure of your data frame, however, applying a conditional styling is possible in pandas through pd.DataFrame.style feature.

I used a subset of you DataFrame to illustrate how to apply bold font on specific cells.

Assuming df looks like this:

    2019    2018
0   Country/INR Header_1
1   Netherlands 3,661
2   Croatia 396
3   Belgium 2,247
newDf = (df
         .style # Access the styling methods for a DataFrame
          .applymap(lambda x: 'font-weight : bold', # Change the font-weight to bold
                    subset=pd.IndexSlice[2:2, ['2019']])) # return a pd Styler object)

newDf.render() # returns formatted HTML

 <style type="text/css" > #T_57b07620_cc86_11e9_82f9_acde48001122row2_col0 { font-weight : bold; font-weight : bold; }</style><table id="T_57b07620_cc86_11e9_82f9_acde48001122" ><thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >2019</th> <th class="col_heading level0 col1" >2018</th> </tr></thead><tbody> <tr> <th id="T_57b07620_cc86_11e9_82f9_acde48001122level0_row0" class="row_heading level0 row0" >0</th> <td id="T_57b07620_cc86_11e9_82f9_acde48001122row0_col0" class="data row0 col0" >Country/INR</td> <td id="T_57b07620_cc86_11e9_82f9_acde48001122row0_col1" class="data row0 col1" >Header_1</td> </tr> <tr> <th id="T_57b07620_cc86_11e9_82f9_acde48001122level0_row1" class="row_heading level0 row1" >1</th> <td id="T_57b07620_cc86_11e9_82f9_acde48001122row1_col0" class="data row1 col0" >Netherlands</td> <td id="T_57b07620_cc86_11e9_82f9_acde48001122row1_col1" class="data row1 col1" >3,661</td> </tr> <tr> <th id="T_57b07620_cc86_11e9_82f9_acde48001122level0_row2" class="row_heading level0 row2" >2</th> <td id="T_57b07620_cc86_11e9_82f9_acde48001122row2_col0" class="data row2 col0" >Croatia</td> <td id="T_57b07620_cc86_11e9_82f9_acde48001122row2_col1" class="data row2 col1" >396</td> </tr> <tr> <th id="T_57b07620_cc86_11e9_82f9_acde48001122level0_row3" class="row_heading level0 row3" >3</th> <td id="T_57b07620_cc86_11e9_82f9_acde48001122row3_col0" class="data row3 col0" >Belgium</td> <td id="T_57b07620_cc86_11e9_82f9_acde48001122row3_col1" class="data row3 col1" >2,247</td> </tr> </tbody></table> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM