{"id":1022214,"date":"2024-12-27T13:31:48","date_gmt":"2024-12-27T05:31:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1022214.html"},"modified":"2024-12-27T13:31:50","modified_gmt":"2024-12-27T05:31:50","slug":"python-pandas%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e9%a2%9c%e8%89%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1022214.html","title":{"rendered":"python pandas\u5982\u4f55\u8bbe\u7f6e\u989c\u8272"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25164600\/99e81f06-7aa7-4fa1-a0cb-b4acdc51cae3.webp\" alt=\"python pandas\u5982\u4f55\u8bbe\u7f6e\u989c\u8272\" \/><\/p>\n<p><p> \u5728\u4f7f\u7528Python\u7684Pandas\u5e93\u8fdb\u884c\u6570\u636e\u5206\u6790\u65f6\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8bbe\u7f6e\u6570\u636e\u6846\u7684\u989c\u8272<\/strong>\uff0c\u4ee5\u4fbf\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u6570\u636e\u3001\u7a81\u51fa\u91cd\u8981\u4fe1\u606f\u3001\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u7b49\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pandas\u7684<code>Styler<\/code>\u5bf9\u8c61\u3001\u7ed3\u5408Matplotlib\u6216Seaborn\u8fdb\u884c\u53ef\u89c6\u5316\u3001\u4ee5\u53ca\u5229\u7528Jupyter Notebook\u7684\u7279\u6027\u8fdb\u884c\u4ea4\u4e92\u5f0f\u5c55\u793a\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528Pandas\u7684<code>Styler<\/code>\u5bf9\u8c61\u662f\u4e00\u79cd\u975e\u5e38\u76f4\u89c2\u4e14\u5f3a\u5927\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6837\u5f0f\u548c\u683c\u5f0f\u5316\u9009\u9879\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u6709\u6548\u5730\u5e94\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Pandas Styler\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>Pandas\u7684<code>Styler<\/code>\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\u6765\u8bbe\u7f6e\u6570\u636e\u6846\u7684\u663e\u793a\u6837\u5f0f\u3002\u901a\u8fc7<code>Styler<\/code>\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8bbe\u7f6e\u989c\u8272\u3001\u5b57\u4f53\u6837\u5f0f\u3001\u683c\u5f0f\u5316\u6570\u636e\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>Styler<\/code>\u5bf9\u8c61\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u901a\u8fc7<code>DataFrame<\/code>\u7684<code>style<\/code>\u5c5e\u6027\u83b7\u53d6<code>Styler<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528\u5404\u79cd\u6837\u5f0f\u65b9\u6cd5\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f<code>highlight_max<\/code>\u548c<code>highlight_min<\/code>\uff0c\u7528\u4e8e\u7a81\u51fa\u663e\u793a\u6570\u636e\u6846\u4e2d\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame(np.random.randn(10, 4), columns=list(&#39;ABCD&#39;))<\/p>\n<h2><strong>\u4f7f\u7528highlight_max\u7a81\u51fa\u663e\u793a\u6700\u5927\u503c<\/strong><\/h2>\n<p>styled_df = df.style.highlight_max(axis=0, color=&#39;lightgreen&#39;)<\/p>\n<p>styled_df<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u968f\u673a\u6570\u7684\u6570\u636e\u6846\uff0c\u5e76\u4f7f\u7528<code>highlight_max<\/code>\u65b9\u6cd5\u5c06\u6bcf\u5217\u4e2d\u7684\u6700\u5927\u503c\u9ad8\u4eae\u663e\u793a\u4e3a\u6d45\u7eff\u8272\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u5e94\u7528\u81ea\u5b9a\u4e49\u6837\u5f0f<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u7f6e\u7684\u6837\u5f0f\u65b9\u6cd5\uff0c<code>Styler<\/code>\u8fd8\u5141\u8bb8\u6211\u4eec\u5e94\u7528\u81ea\u5b9a\u4e49\u7684\u6837\u5f0f\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u8fd4\u56de\u4e00\u4e2a\u6837\u5f0f\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u6b64\u51fd\u6570\u5e94\u7528\u4e8e\u6570\u636e\u6846\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def highlight_greater_than(s, threshold, color=&#39;yellow&#39;):<\/p>\n<p>    is_greater = s &gt; threshold<\/p>\n<p>    return [&#39;background-color: {}&#39;.format(color) if v else &#39;&#39; for v in is_greater]<\/p>\n<p>styled_df = df.style.apply(highlight_greater_than, threshold=0.5, color=&#39;yellow&#39;, axis=0)<\/p>\n<p>styled_df<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>highlight_greater_than<\/code>\uff0c\u5b83\u4f1a\u5c06\u6570\u636e\u6846\u4e2d\u5927\u4e8e\u67d0\u4e2a\u9608\u503c\u7684\u5143\u7d20\u9ad8\u4eae\u663e\u793a\u3002\u6211\u4eec\u5c06\u6b64\u51fd\u6570\u5e94\u7528\u4e8e\u6570\u636e\u6846\u4ee5\u7a81\u51fa\u663e\u793a\u5927\u4e8e0.5\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7ed3\u5408Matplotlib\u6216Seaborn\u8fdb\u884c\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528Pandas\u81ea\u8eab\u7684<code>Styler<\/code>\u5bf9\u8c61\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u7ed3\u5408Matplotlib\u6216Seaborn\u7b49\u53ef\u89c6\u5316\u5e93\u6765\u8bbe\u7f6e\u989c\u8272\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u53ef\u89c6\u5316\u529f\u80fd\uff0c\u53ef\u4ee5\u751f\u6210\u56fe\u5f62\u5316\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528Matplotlib<\/h4>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u53ef\u89c6\u5316\u5e93\u4e4b\u4e00\uff0c\u5b83\u4e0ePandas\u7d27\u5bc6\u96c6\u6210\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528Matplotlib\u521b\u5efa\u5404\u79cd\u56fe\u8868\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6e\u989c\u8272\u53c2\u6570\u6765\u6539\u53d8\u56fe\u8868\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>df[&#39;A&#39;].plot(kind=&#39;bar&#39;, color=&#39;skyblue&#39;)<\/p>\n<p>plt.title(&#39;Column A Bar Chart&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e86\u6570\u636e\u6846<code>A<\/code>\u5217\u7684\u67f1\u72b6\u56fe\uff0c\u5e76\u8bbe\u7f6e\u4e86\u67f1\u5b50\u7684\u989c\u8272\u4e3a\u5929\u84dd\u8272\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528Seaborn<\/h4>\n<\/p>\n<p><p>Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u56fe\u8868\u6837\u5f0f\u548c\u989c\u8272\u9009\u9879\u3002\u5b83\u4f7f\u5f97\u521b\u5efa\u7f8e\u89c2\u7684\u7edf\u8ba1\u56fe\u8868\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u70ed\u529b\u56fe<\/strong><\/h2>\n<p>sns.heatmap(df.corr(), annot=True, cmap=&#39;coolwarm&#39;)<\/p>\n<p>plt.title(&#39;Correlation Heatmap&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528Seaborn\u521b\u5efa\u4e86\u4e00\u4e2a\u70ed\u529b\u56fe\uff0c\u5c55\u793a\u4e86\u6570\u636e\u6846\u4e2d\u5404\u5217\u4e4b\u95f4\u7684\u76f8\u5173\u6027\u3002<code>cmap<\/code>\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u70ed\u529b\u56fe\u7684\u989c\u8272\u6620\u5c04\uff0c\u6211\u4eec\u9009\u62e9\u4e86\u4e00\u4e2a\u4ece\u51b7\u5230\u6696\u7684\u989c\u8272\u6e10\u53d8\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528Jupyter Notebook\u7684\u7279\u6027\u8fdb\u884c\u4ea4\u4e92\u5f0f\u5c55\u793a<\/h3>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5176\u7279\u6027\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u6846\u3002\u867d\u7136\u8fd9\u4e0d\u662f\u76f4\u63a5\u6539\u53d8\u989c\u8272\u7684\u65b9\u6cd5\uff0c\u4f46\u901a\u8fc7\u7ed3\u5408<code>IPython.display<\/code>\u6a21\u5757\u548cHTML\/CSS\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6837\u5f0f\u548c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528HTML\u548cCSS<\/h4>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u6570\u636e\u6846\u8f6c\u6362\u4e3aHTML\uff0c\u5e76\u4f7f\u7528CSS\u6765\u8bbe\u7f6e\u6837\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u66f4\u590d\u6742\u6837\u5f0f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from IPython.core.display import HTML<\/p>\n<h2><strong>\u5c06\u6570\u636e\u6846\u8f6c\u6362\u4e3aHTML\u5e76\u5e94\u7528CSS<\/strong><\/h2>\n<p>html = df.style.set_table_styles(<\/p>\n<p>    [{&#39;selector&#39;: &#39;tr:hover&#39;,<\/p>\n<p>      &#39;props&#39;: [(&#39;background-color&#39;, &#39;#f0f0f0&#39;)]}]<\/p>\n<p>).render()<\/p>\n<p>display(HTML(html))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u6570\u636e\u6846\u8f6c\u6362\u4e3aHTML\uff0c\u5e76\u4f7f\u7528CSS\u5728\u9f20\u6807\u60ac\u505c\u65f6\u6539\u53d8\u884c\u7684\u80cc\u666f\u8272\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528\u4ea4\u4e92\u5f0f\u5c0f\u90e8\u4ef6<\/h4>\n<\/p>\n<p><p>Jupyter Notebook\u652f\u6301\u4f7f\u7528<code>ipywidgets<\/code>\u5e93\u521b\u5efa\u4ea4\u4e92\u5f0f\u5c0f\u90e8\u4ef6\uff0c\u8fd9\u4e9b\u5c0f\u90e8\u4ef6\u53ef\u4ee5\u4e0ePandas\u6570\u636e\u6846\u7ed3\u5408\u4f7f\u7528\u4ee5\u521b\u5efa\u52a8\u6001\u7684\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ipywidgets as widgets<\/p>\n<p>from IPython.display import display<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6ed1\u5757\u5c0f\u90e8\u4ef6<\/strong><\/h2>\n<p>slider = widgets.FloatSlider(value=0.5, min=0, max=1, step=0.1, description=&#39;Threshold:&#39;)<\/p>\n<p>def update(threshold):<\/p>\n<p>    display(df.style.apply(highlight_greater_than, threshold=threshold, color=&#39;yellow&#39;, axis=0))<\/p>\n<p>widgets.interactive(update, threshold=slider)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6ed1\u5757\u5c0f\u90e8\u4ef6\uff0c\u5141\u8bb8\u7528\u6237\u52a8\u6001\u8c03\u6574\u9608\u503c\uff0c\u5e76\u5b9e\u65f6\u66f4\u65b0\u6570\u636e\u6846\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u8df5\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8bbe\u7f6e\u6570\u636e\u6846\u7684\u989c\u8272\u6709\u52a9\u4e8e\u63d0\u9ad8\u6570\u636e\u5206\u6790\u7684\u6548\u7387\u548c\u7ed3\u679c\u7684\u53ef\u8bfb\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u6570\u636e\u63a2\u7d22\u548c\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u63a2\u7d22\u548c\u5206\u6790\u9636\u6bb5\uff0c\u4f7f\u7528\u989c\u8272\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u8bc6\u522b\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u503c\u3001\u8d8b\u52bf\u548c\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u5206\u6790\u9500\u552e\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u989c\u8272\u7a81\u51fa\u663e\u793a\u9500\u552e\u989d\u7684\u589e\u957f\u6216\u4e0b\u964d\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u9500\u552e\u6570\u636e\u6846<\/p>\n<p>sales_data = pd.DataFrame({<\/p>\n<p>    &#39;Month&#39;: [&#39;Jan&#39;, &#39;Feb&#39;, &#39;Mar&#39;, &#39;Apr&#39;],<\/p>\n<p>    &#39;Sales&#39;: [200, 220, 250, 230]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u7528\u989c\u8272\u663e\u793a\u9500\u552e\u8d8b\u52bf<\/strong><\/h2>\n<p>sales_data.style.bar(subset=[&#39;Sales&#39;], color=&#39;lightblue&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u6570\u636e\u62a5\u544a\u548c\u5c55\u793a<\/h4>\n<\/p>\n<p><p>\u5728\u5236\u4f5c\u6570\u636e\u62a5\u544a\u548c\u5c55\u793a\u65f6\uff0c\u4f7f\u7528\u989c\u8272\u53ef\u4ee5\u589e\u5f3a\u62a5\u544a\u7684\u89c6\u89c9\u6548\u679c\uff0c\u4f7f\u5f97\u53d7\u4f17\u66f4\u5bb9\u6613\u7406\u89e3\u6570\u636e\u7684\u542b\u4e49\u3002\u4f8b\u5982\uff0c\u5728\u8d22\u52a1\u62a5\u544a\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ea2\u8272\u548c\u7eff\u8272\u5206\u522b\u8868\u793a\u4e8f\u635f\u548c\u76c8\u5229\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u8d22\u52a1\u62a5\u544a\u6570\u636e\u6846<\/p>\n<p>finance_data = pd.DataFrame({<\/p>\n<p>    &#39;Category&#39;: [&#39;Revenue&#39;, &#39;Expense&#39;, &#39;Profit&#39;],<\/p>\n<p>    &#39;Amount&#39;: [1000, 800, 200]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u7528\u989c\u8272\u8868\u793a\u8d22\u52a1\u72b6\u51b5<\/strong><\/h2>\n<p>def color_negative_red(val):<\/p>\n<p>    color = &#39;red&#39; if val &lt; 0 else &#39;green&#39;<\/p>\n<p>    return &#39;color: {}&#39;.format(color)<\/p>\n<p>finance_data.style.applymap(color_negative_red, subset=[&#39;Amount&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>color_negative_red<\/code>\uff0c\u7528\u6765\u6839\u636e\u503c\u7684\u7b26\u53f7\u8bbe\u7f6e\u6587\u672c\u989c\u8272\uff0c\u4ece\u800c\u76f4\u89c2\u5730\u5c55\u793a\u8d22\u52a1\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u5c55\u671b<\/h3>\n<\/p>\n<p><p>\u4e3aPandas\u6570\u636e\u6846\u8bbe\u7f6e\u989c\u8272\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u5c55\u793a\u6570\u636e\u3002\u901a\u8fc7<code>Styler<\/code>\u5bf9\u8c61\u3001\u7ed3\u5408Matplotlib\u6216Seaborn\u7b49\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u4ee5\u53ca\u5229\u7528Jupyter Notebook\u7684\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5e94\u7528\u989c\u8272\u6765\u589e\u5f3a\u6570\u636e\u5206\u6790\u7684\u6548\u679c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u6709\u6548\u5730\u5448\u73b0\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u968f\u7740\u6570\u636e\u79d1\u5b66\u7684\u53d1\u5c55\u548c\u5de5\u5177\u7684\u4e0d\u65ad\u6539\u8fdb\uff0c\u672a\u6765\u6211\u4eec\u53ef\u4ee5\u671f\u5f85\u66f4\u52a0\u667a\u80fd\u5316\u548c\u81ea\u52a8\u5316\u7684\u6837\u5f0f\u8bbe\u7f6e\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u501f\u52a9<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b97\u6cd5\uff0c\u81ea\u52a8\u8bc6\u522b\u6570\u636e\u4e2d\u7684\u91cd\u8981\u7279\u5f81\u5e76\u8fdb\u884c\u89c6\u89c9\u4e0a\u7684\u7a81\u51fa\u663e\u793a\u3002\u603b\u4e4b\uff0c\u5408\u7406\u5730\u5229\u7528\u989c\u8272\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u6570\u636e\u5206\u6790\u7684\u6548\u7387\uff0c\u8fd8\u80fd\u8ba9\u6570\u636e\u6545\u4e8b\u66f4\u52a0\u751f\u52a8\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python Pandas\u4e2d\u4e3a\u6570\u636e\u6846\u7684\u7279\u5b9a\u5355\u5143\u683c\u8bbe\u7f6e\u989c\u8272\uff1f<\/strong><br \/>\u5728Python Pandas\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Styler<\/code>\u5bf9\u8c61\u6765\u4e3a\u6570\u636e\u6846\u7684\u7279\u5b9a\u5355\u5143\u683c\u8bbe\u7f6e\u989c\u8272\u3002\u901a\u8fc7<code>apply<\/code>\u548c<code>applymap<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u6539\u53d8\u5355\u5143\u683c\u7684\u80cc\u666f\u8272\u6216\u5b57\u4f53\u8272\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u5355\u5143\u683c\u7684\u503c\uff0c\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u989c\u8272\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndef highlight_positive(val):\n    color = &#39;background-color: green&#39; if val &gt; 0 else &#39;&#39;\n    return color\n\ndf = pd.DataFrame({&#39;A&#39;: [1, -1, 2], &#39;B&#39;: [3, -3, 4]})\nstyled_df = df.style.applymap(highlight_positive)\n<\/code><\/pre>\n<p><strong>\u5728Pandas\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u6761\u4ef6\u683c\u5f0f\u5316\u6765\u6539\u5584\u6570\u636e\u7684\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u6761\u4ef6\u683c\u5f0f\u5316\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u6570\u636e\u66f4\u5177\u53ef\u8bfb\u6027\u3002\u5728Pandas\u4e2d\uff0c\u901a\u8fc7<code>Styler<\/code>\u5bf9\u8c61\u53ef\u4ee5\u5e94\u7528\u591a\u79cd\u6837\u5f0f\uff0c\u4f8b\u5982\u6539\u53d8\u5b57\u4f53\u3001\u80cc\u666f\u989c\u8272\u6216\u6587\u672c\u6837\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>where<\/code>\u65b9\u6cd5\u6765\u57fa\u4e8e\u6761\u4ef6\u8bbe\u7f6e\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u6240\u6709\u8d1f\u503c\u7684\u5355\u5143\u683c\u80cc\u666f\u8bbe\u7f6e\u4e3a\u7ea2\u8272\uff0c\u4ee5\u4fbf\u5feb\u901f\u8bc6\u522b\uff1a<\/p>\n<pre><code class=\"language-python\">styled_df = df.style.where(df &lt; 0, &#39;background-color: red&#39;)\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u5728Pandas\u4e2d\u4f7f\u7528\u54ea\u4e9b\u989c\u8272\u683c\u5f0f\u6765\u8bbe\u7f6e\u6837\u5f0f\uff1f<\/strong><br \/>\u5728Pandas\u7684\u6837\u5f0f\u8bbe\u7f6e\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u989c\u8272\u683c\u5f0f\u6765\u6307\u5b9a\u989c\u8272\uff0c\u5305\u62ec\u5e38\u89c1\u7684\u989c\u8272\u540d\u79f0\uff08\u5982&#39;blue&#39;\u3001&#39;red&#39;\u3001&#39;green&#39;\u7b49\uff09\uff0c\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\uff08\u5982&#39;#FF5733&#39;\uff09\uff0c\u4ee5\u53caRGB\u989c\u8272\u503c\uff08\u5982&#39;rgb(255, 87, 51)&#39;\uff09\u3002\u8fd9\u4f7f\u5f97\u7528\u6237\u80fd\u591f\u7075\u6d3b\u5730\u9009\u62e9\u9002\u5408\u5176\u9700\u6c42\u7684\u989c\u8272\u65b9\u6848\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">styled_df = df.style.applymap(lambda x: &#39;color: red&#39; if x &lt; 0 else &#39;color: black&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u4e3a\u6570\u636e\u6846\u7684\u5355\u5143\u683c\u8bbe\u7f6e\u6837\u5f0f\uff0c\u4f7f\u6570\u636e\u66f4\u52a0\u76f4\u89c2\u6613\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u4f7f\u7528Python\u7684Pandas\u5e93\u8fdb\u884c\u6570\u636e\u5206\u6790\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8bbe\u7f6e\u6570\u636e\u6846\u7684\u989c\u8272\uff0c\u4ee5\u4fbf\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u6570\u636e\u3001\u7a81\u51fa [&hellip;]","protected":false},"author":3,"featured_media":1022221,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1022214"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1022214"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1022214\/revisions"}],"predecessor-version":[{"id":1022225,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1022214\/revisions\/1022225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1022221"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1022214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1022214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1022214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}