{"id":1062595,"date":"2024-12-31T15:53:22","date_gmt":"2024-12-31T07:53:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1062595.html"},"modified":"2024-12-31T15:53:24","modified_gmt":"2024-12-31T07:53:24","slug":"python%e4%b8%adpanda%e5%a6%82%e4%bd%95%e5%af%b9%e4%b8%ad%e9%97%b4%e5%80%bc%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1062595.html","title":{"rendered":"python\u4e2dpanda\u5982\u4f55\u5bf9\u4e2d\u95f4\u503c\u64cd\u4f5c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/4913a289-6896-4776-af80-60c44ae86ba2.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2dpanda\u5982\u4f55\u5bf9\u4e2d\u95f4\u503c\u64cd\u4f5c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u5bf9\u4e2d\u95f4\u503c\u8fdb\u884c\u64cd\u4f5c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>median()<\/code>\u51fd\u6570\u8ba1\u7b97\u4e2d\u95f4\u503c\u3001\u5bf9\u4e2d\u95f4\u503c\u8fdb\u884c\u6761\u4ef6\u7b5b\u9009\u3001\u63d2\u503c\u5904\u7406\u3001\u4f7f\u7528<code>rank()<\/code>\u51fd\u6570\u8fdb\u884c\u6392\u540d\u7b49\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528<code>median()<\/code>\u51fd\u6570\u8ba1\u7b97\u4e2d\u95f4\u503c\u5e76\u8fdb\u884c\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p>Pandas\u5e93\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u3001\u65b9\u4fbf\u7684\u6570\u636e\u7ed3\u6784\u6765\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u901a\u8fc7Pandas\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\u5bf9\u4e2d\u95f4\u503c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8ba1\u7b97\u4e2d\u95f4\u503c<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u4e2d\u7684<code>median()<\/code>\u51fd\u6570\u53ef\u4ee5\u8ba1\u7b97\u6570\u636e\u7684\u4e2d\u4f4d\u6570\uff08\u4e2d\u95f4\u503c\uff09\u3002\u4e2d\u4f4d\u6570\u662f\u5c06\u4e00\u7ec4\u6570\u636e\u6309\u5927\u5c0f\u987a\u5e8f\u6392\u5217\u540e\uff0c\u4f4d\u4e8e\u4e2d\u95f4\u7684\u90a3\u4e2a\u6570\u3002\u5bf9\u4e8e\u5947\u6570\u4e2a\u6570\u636e\uff0c\u4e2d\u4f4d\u6570\u662f\u4e2d\u95f4\u90a3\u4e2a\u6570\uff1b\u5bf9\u4e8e\u5076\u6570\u4e2a\u6570\u636e\uff0c\u4e2d\u4f4d\u6570\u662f\u4e2d\u95f4\u4e24\u4e2a\u6570\u7684\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;values&#39;: [1, 2, 3, 4, 5, 6, 7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u4e2d\u4f4d\u6570<\/strong><\/h2>\n<p>median_value = df[&#39;values&#39;].median()<\/p>\n<p>print(&quot;\u4e2d\u4f4d\u6570:&quot;, median_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bf9\u4e2d\u95f4\u503c\u8fdb\u884c\u6761\u4ef6\u7b5b\u9009<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e2d\u95f4\u503c\u5bf9\u6570\u636e\u8fdb\u884c\u6761\u4ef6\u7b5b\u9009\uff0c\u4f8b\u5982\u7b5b\u9009\u51fa\u5927\u4e8e\u4e2d\u4f4d\u6570\u7684\u503c\u6216\u5c0f\u4e8e\u4e2d\u4f4d\u6570\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b5b\u9009\u51fa\u5927\u4e8e\u4e2d\u4f4d\u6570\u7684\u503c<\/p>\n<p>greater_than_median = df[df[&#39;values&#39;] &gt; median_value]<\/p>\n<p>print(&quot;\u5927\u4e8e\u4e2d\u4f4d\u6570\u7684\u503c:\\n&quot;, greater_than_median)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u5c0f\u4e8e\u4e2d\u4f4d\u6570\u7684\u503c<\/strong><\/h2>\n<p>less_than_median = df[df[&#39;values&#39;] &lt; median_value]<\/p>\n<p>print(&quot;\u5c0f\u4e8e\u4e2d\u4f4d\u6570\u7684\u503c:\\n&quot;, less_than_median)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u63d2\u503c\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u7684\u6570\u636e\u53ef\u80fd\u5305\u542b\u7f3a\u5931\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e2d\u95f4\u503c\u6765\u586b\u5145\u8fd9\u4e9b\u7f3a\u5931\u503c\u3002Pandas\u5e93\u63d0\u4f9b\u4e86<code>fillna()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7f3a\u5931\u503c\u7684DataFrame<\/p>\n<p>data_with_nan = {&#39;values&#39;: [1, 2, None, 4, None, 6, 7, 8, 9]}<\/p>\n<p>df_with_nan = pd.DataFrame(data_with_nan)<\/p>\n<h2><strong>\u4f7f\u7528\u4e2d\u4f4d\u6570\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df_filled = df_with_nan.fillna(median_value)<\/p>\n<p>print(&quot;\u586b\u5145\u540e\u7684DataFrame:\\n&quot;, df_filled)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>rank()<\/code>\u51fd\u6570\u8fdb\u884c\u6392\u540d<\/h3>\n<\/p>\n<p><p><code>rank()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u540d\uff0c\u5e76\u4e14\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5904\u7406\u76f8\u540c\u503c\u7684\u6392\u540d\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u4e2d\u95f4\u503c\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bf9\u6570\u636e\u8fdb\u884c\u6392\u540d<\/p>\n<p>df[&#39;rank&#39;] = df[&#39;values&#39;].rank()<\/p>\n<p>print(&quot;\u6392\u540d\u540e\u7684DataFrame:\\n&quot;, df)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u6392\u540d\u5728\u4e2d\u95f4\u503c\u9644\u8fd1\u7684\u6570\u636e<\/strong><\/h2>\n<p>median_rank = df[&#39;rank&#39;].median()<\/p>\n<p>near_median_rank = df[(df[&#39;rank&#39;] &gt;= median_rank - 1) &amp; (df[&#39;rank&#39;] &lt;= median_rank + 1)]<\/p>\n<p>print(&quot;\u6392\u540d\u5728\u4e2d\u95f4\u503c\u9644\u8fd1\u7684\u6570\u636e:\\n&quot;, near_median_rank)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5b66\u751f\u8003\u8bd5\u6210\u7ee9\u7684DataFrame\uff0c\u6211\u4eec\u5e0c\u671b\u5bf9\u8fd9\u4e9b\u6210\u7ee9\u8fdb\u884c\u5206\u6790\uff0c\u627e\u51fa\u6210\u7ee9\u4e2d\u4f4d\u6570\u4ee5\u53ca\u5bf9\u4e2d\u4f4d\u6570\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b66\u751f\u8003\u8bd5\u6210\u7ee9\u7684DataFrame<\/p>\n<p>data_scores = {&#39;scores&#39;: [55, 78, 90, 66, 85, 72, 88, 94, 59, 70]}<\/p>\n<p>df_scores = pd.DataFrame(data_scores)<\/p>\n<h2><strong>\u8ba1\u7b97\u6210\u7ee9\u7684\u4e2d\u4f4d\u6570<\/strong><\/h2>\n<p>median_score = df_scores[&#39;scores&#39;].median()<\/p>\n<p>print(&quot;\u6210\u7ee9\u4e2d\u4f4d\u6570:&quot;, median_score)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u9ad8\u4e8e\u4e2d\u4f4d\u6570\u7684\u6210\u7ee9<\/strong><\/h2>\n<p>high_scores = df_scores[df_scores[&#39;scores&#39;] &gt; median_score]<\/p>\n<p>print(&quot;\u9ad8\u4e8e\u4e2d\u4f4d\u6570\u7684\u6210\u7ee9:\\n&quot;, high_scores)<\/p>\n<h2><strong>\u4f7f\u7528\u4e2d\u4f4d\u6570\u586b\u5145\u7f3a\u5931\u503c\uff08\u5047\u8bbe\u67d0\u4e9b\u5b66\u751f\u7684\u6210\u7ee9\u7f3a\u5931\uff09<\/strong><\/h2>\n<p>data_scores_with_nan = {&#39;scores&#39;: [55, 78, None, 66, 85, None, 88, 94, 59, 70]}<\/p>\n<p>df_scores_with_nan = pd.DataFrame(data_scores_with_nan)<\/p>\n<p>df_scores_filled = df_scores_with_nan.fillna(median_score)<\/p>\n<p>print(&quot;\u586b\u5145\u540e\u7684\u6210\u7ee9:\\n&quot;, df_scores_filled)<\/p>\n<h2><strong>\u5bf9\u6210\u7ee9\u8fdb\u884c\u6392\u540d<\/strong><\/h2>\n<p>df_scores[&#39;rank&#39;] = df_scores[&#39;scores&#39;].rank()<\/p>\n<p>print(&quot;\u6392\u540d\u540e\u7684\u6210\u7ee9:\\n&quot;, df_scores)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u6392\u540d\u5728\u4e2d\u95f4\u503c\u9644\u8fd1\u7684\u6210\u7ee9<\/strong><\/h2>\n<p>median_rank_score = df_scores[&#39;rank&#39;].median()<\/p>\n<p>near_median_rank_scores = df_scores[(df_scores[&#39;rank&#39;] &gt;= median_rank_score - 1) &amp; (df_scores[&#39;rank&#39;] &lt;= median_rank_score + 1)]<\/p>\n<p>print(&quot;\u6392\u540d\u5728\u4e2d\u95f4\u503c\u9644\u8fd1\u7684\u6210\u7ee9:\\n&quot;, near_median_rank_scores)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u51faPandas\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u5bf9\u4e2d\u95f4\u503c\u8fdb\u884c\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u8ba1\u7b97\u4e2d\u4f4d\u6570\u3001\u6761\u4ef6\u7b5b\u9009\u3001\u63d2\u503c\u5904\u7406\u8fd8\u662f\u6392\u540d\u64cd\u4f5c\uff0cPandas\u90fd\u80fd\u8f7b\u677e\u5e94\u5bf9\u3002\u5728\u5b9e\u9645\u6570\u636e\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u5bf9\u4e2d\u95f4\u503c\u7684\u64cd\u4f5c\u662f\u975e\u5e38\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u638c\u63e1\u8fd9\u4e9b\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\uff0c\u63d0\u9ad8\u6570\u636e\u5206\u6790\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Pandas\u5bf9\u4e2d\u95f4\u503c\u8fdb\u884c\u64cd\u4f5c\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u7591\u95ee\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u8ba8\u8bba\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Pandas\u5e93\u8ba1\u7b97\u6570\u636e\u7684\u4e2d\u4f4d\u6570\uff1f<\/strong><br \/>\u5728Pandas\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>median()<\/code>\u51fd\u6570\u8f7b\u677e\u8ba1\u7b97\u6570\u636e\u7684\u4e2d\u4f4d\u6570\u3002\u9996\u5148\uff0c\u9700\u8981\u5c06\u6570\u636e\u52a0\u8f7d\u5230DataFrame\u4e2d\uff0c\u7136\u540e\u53ef\u4ee5\u9488\u5bf9\u7279\u5b9a\u7684\u5217\u8c03\u7528<code>median()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c<code>df[&#39;column_name&#39;].median()<\/code>\u5c06\u8fd4\u56de\u8be5\u5217\u7684\u4e2d\u4f4d\u6570\u503c\u3002\u8fd9\u5728\u5904\u7406\u6570\u636e\u96c6\u4e2d\u5b58\u5728\u6781\u7aef\u503c\u6216\u504f\u6001\u5206\u5e03\u65f6\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3a\u4e2d\u4f4d\u6570\u80fd\u66f4\u597d\u5730\u53cd\u6620\u6570\u636e\u7684\u4e2d\u5fc3\u8d8b\u52bf\u3002<\/p>\n<p><strong>Pandas\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\u4ee5\u8ba1\u7b97\u4e2d\u4f4d\u6570\uff1f<\/strong><br \/>\u5728\u8ba1\u7b97\u4e2d\u4f4d\u6570\u65f6\uff0cPandas\u9ed8\u8ba4\u4f1a\u5ffd\u7565\u7f3a\u5931\u503c\uff08NaN\uff09\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u6570\u636e\u96c6\u4e2d\u5b58\u5728\u7f3a\u5931\u7684\u503c\uff0c<code>median()<\/code>\u51fd\u6570\u4f1a\u81ea\u52a8\u8df3\u8fc7\u8fd9\u4e9b\u503c\uff0c\u5e76\u4ec5\u57fa\u4e8e\u5b58\u5728\u7684\u503c\u8fdb\u884c\u8ba1\u7b97\u3002\u5982\u679c\u5e0c\u671b\u5728\u8ba1\u7b97\u4e2d\u4f4d\u6570\u4e4b\u524d\u586b\u5145\u7f3a\u5931\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\uff0c\u4f8b\u5982<code>df[&#39;column_name&#39;].fillna(value).median()<\/code>\uff0c\u5176\u4e2d<code>value<\/code>\u662f\u60a8\u5e0c\u671b\u7528\u6765\u66ff\u4ee3\u7f3a\u5931\u503c\u7684\u6570\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Pandas\u4e2d\u5bf9\u5206\u7ec4\u6570\u636e\u8ba1\u7b97\u4e2d\u4f4d\u6570\uff1f<\/strong><br \/>Pandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5206\u7ec4\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528<code>groupby()<\/code>\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u518d\u8ba1\u7b97\u6bcf\u7ec4\u7684\u4e2d\u4f4d\u6570\u3002\u53ef\u4ee5\u6309\u7167\u67d0\u4e00\u5217\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u5bf9\u53e6\u4e00\u5217\u8ba1\u7b97\u4e2d\u4f4d\u6570\u3002\u4f8b\u5982\uff0c<code>df.groupby(&#39;group_column&#39;)[&#39;value_column&#39;].median()<\/code>\u5c06\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u7684\u4e2d\u4f4d\u6570\u3002\u8fd9\u5bf9\u4e8e\u5206\u6790\u4e0d\u540c\u7c7b\u522b\u6216\u7ec4\u4e4b\u95f4\u7684\u5dee\u5f02\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u5bf9\u4e2d\u95f4\u503c\u8fdb\u884c\u64cd\u4f5c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528median()\u51fd\u6570\u8ba1 [&hellip;]","protected":false},"author":3,"featured_media":1062604,"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\/1062595"}],"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=1062595"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062595\/revisions"}],"predecessor-version":[{"id":1062608,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062595\/revisions\/1062608"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1062604"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1062595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1062595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1062595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}