首页 > 解决方案 > pandas 数据框外观的变化

问题描述

我有一个数据框,我需要更改该数据框的外观以通过电子邮件发送该数据框。我需要更改各个行的颜色,需要在数据框之间使某些行加粗。

输入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%

在此处输入图像描述

需要输出:-

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%

在此处输入图像描述

如果有人对数据框进行了一些 HTML 更改。请与我分享。(如何使用df.to_html)

提前致谢。

标签: pythonpandasdataframe

解决方案


我不了解您的数据框的结构,但是,通过pd.DataFrame.style功能在熊猫中应用条件样式是可能的。

我用你们的一个子集DataFrame来说明如何在特定单元格上应用粗体。

假设df看起来像这样:

    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>


推荐阅读