python - 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)
提前致谢。
解决方案
我不了解您的数据框的结构,但是,通过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>
推荐阅读
- android - Flutter - 我们可以忽略/限制/控制“系统显示大小”设置吗?
- excel - 创建excel图表显示该类别的实现值与标准集?
- mysql - 无法将在 docker 上运行的 MySQL 服务器与 Node-Red 容器连接起来
- c# - 相机在应用鼠标旋转之前重置键盘旋转
- spring-security - 为什么oauthtoken response_body中的“access_token”改为“value”?
- plot - 使用 K-means 缩放后如何使用原始变量绘制散点图
- python - 如何对补丁大小内的点云数据集群求和
- flutter - 更改 Positioned in Stack 的父级并将其从前一个父级的位置平滑地转换为新的父级
- python-3.x - Facebook Ads Insights - 过滤昨天活跃的广告
- docker - 在 Docker-compose 中声明 Jenkins slave