{"id":1030846,"date":"2024-12-31T11:20:33","date_gmt":"2024-12-31T03:20:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1030846.html"},"modified":"2024-12-31T11:20:35","modified_gmt":"2024-12-31T03:20:35","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e6%b1%82%e6%9c%80%e4%bd%8e%e5%b7%a5%e8%b5%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1030846.html","title":{"rendered":"\u7528python\u5982\u4f55\u6c42\u6700\u4f4e\u5de5\u8d44"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/cce200ce-85f0-4e55-84df-928e20aab40d.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u7528python\u5982\u4f55\u6c42\u6700\u4f4e\u5de5\u8d44\" \/><\/p>\n<p><p> <strong>\u7528Python\u5982\u4f55\u6c42\u6700\u4f4e\u5de5\u8d44\uff1f<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5229\u7528Python\u6765\u8ba1\u7b97\u6700\u4f4e\u5de5\u8d44\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u8bfb\u53d6\u5de5\u8d44\u6570\u636e\u6587\u4ef6\u3001\u4ece\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u6570\u636e\u3001\u4f7f\u7528API\u83b7\u53d6\u6570\u636e\u7b49\u3002\u901a\u8fc7\u6570\u636e\u5904\u7406\u548c\u5206\u6790\uff0c\u53ef\u4ee5\u6c42\u51fa\u6700\u4f4e\u5de5\u8d44\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u53ca\u5176\u76f8\u5173\u5e93\u6765\u6c42\u6700\u4f4e\u5de5\u8d44\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bfb\u53d6\u5de5\u8d44\u6570\u636e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u51c6\u5907\u597d\u5de5\u8d44\u6570\u636e\u6587\u4ef6\u3002\u5e38\u89c1\u7684\u6570\u636e\u683c\u5f0f\u5305\u62ecCSV\u3001Excel\u3001JSON\u7b49\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>salaries.csv<\/code>\u7684CSV\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u5458\u5de5\u7684\u5de5\u8d44\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;salaries.csv&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u6570\u636e\u7684\u524d\u51e0\u884c<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u6587\u4ef6\u53ef\u80fd\u5305\u542b\u4ee5\u4e0b\u5217\uff1a<code>EmployeeID<\/code>\u3001<code>Name<\/code>\u3001<code>Department<\/code>\u3001<code>Salary<\/code>\u3002\u901a\u8fc7Pandas\u5e93\u8bfb\u53d6\u6570\u636e\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>head()<\/code>\u65b9\u6cd5\u67e5\u770b\u524d\u51e0\u884c\u6570\u636e\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u8bfb\u53d6\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u6b63\u786e\u6027\u3002\u53ef\u4ee5\u68c0\u67e5\u662f\u5426\u6709\u7f3a\u5931\u503c\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u662f\u5426\u6709\u7f3a\u5931\u503c<\/p>\n<p>print(data.isnull().sum())<\/p>\n<h2><strong>\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/strong><\/h2>\n<p>data = data.dropna()<\/p>\n<h2><strong>\u786e\u4fdd\u5de5\u8d44\u5217\u662f\u6570\u503c\u7c7b\u578b<\/strong><\/h2>\n<p>data[&#39;Salary&#39;] = pd.to_numeric(data[&#39;Salary&#39;], errors=&#39;coerce&#39;)<\/p>\n<h2><strong>\u518d\u6b21\u68c0\u67e5\u662f\u5426\u6709\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>data = data.dropna()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6c42\u6700\u4f4e\u5de5\u8d44<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u597d\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>min()<\/code>\u65b9\u6cd5\u6c42\u51fa\u6700\u4f4e\u5de5\u8d44\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6c42\u6700\u4f4e\u5de5\u8d44<\/p>\n<p>min_salary = data[&#39;Salary&#39;].min()<\/p>\n<p>print(f&quot;\u6700\u4f4e\u5de5\u8d44\u662f: {min_salary}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><strong>\u6570\u636e\u6e05\u6d17\u662f\u8ba1\u7b97\u6700\u4f4e\u5de5\u8d44\u7684\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00<\/strong>\u3002\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u5de5\u8d44\u6570\u636e\u53ef\u80fd\u4f1a\u6709\u7f3a\u5931\u503c\u6216\u5f02\u5e38\u503c\u3002\u4f8b\u5982\uff0c\u6709\u4e9b\u5458\u5de5\u7684\u5de5\u8d44\u53ef\u80fd\u672a\u5f55\u5165\uff0c\u6216\u8005\u5f55\u5165\u9519\u8bef\u3002\u901a\u8fc7\u68c0\u67e5\u548c\u5904\u7406\u7f3a\u5931\u503c\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u5de5\u8d44\u5217\u662f\u6570\u503c\u7c7b\u578b\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a\u975e\u6570\u503c\u7c7b\u578b\u7684\u6570\u636e\u65e0\u6cd5\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4ece\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u5916\uff0c\u8fd8\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4eceMySQL\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u5de5\u8d44\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = mysql.connector.connect(<\/p>\n<p>    host=&quot;localhost&quot;,<\/p>\n<p>    user=&quot;username&quot;,<\/p>\n<p>    password=&quot;password&quot;,<\/p>\n<p>    database=&quot;database_name&quot;<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884cSQL\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT Salary FROM salaries&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u7ed3\u679c<\/strong><\/h2>\n<p>salaries = cursor.fetchall()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aPandas DataFrame<\/strong><\/h2>\n<p>data = pd.DataFrame(salaries, columns=[&#39;Salary&#39;])<\/p>\n<h2><strong>\u6c42\u6700\u4f4e\u5de5\u8d44<\/strong><\/h2>\n<p>min_salary = data[&#39;Salary&#39;].min()<\/p>\n<p>print(f&quot;\u6700\u4f4e\u5de5\u8d44\u662f: {min_salary}&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528API\u83b7\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5de5\u8d44\u6570\u636e\u53ef\u80fd\u5b58\u50a8\u5728\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7API\u83b7\u53d6\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528requests\u5e93\u4eceAPI\u83b7\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&quot;https:\/\/api.example.com\/salaries&quot;)<\/p>\n<h2><strong>\u68c0\u67e5\u54cd\u5e94\u72b6\u6001\u7801<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    # \u5c06\u54cd\u5e94\u6570\u636e\u8f6c\u6362\u4e3aPandas DataFrame<\/p>\n<p>    data = pd.DataFrame(response.json())<\/p>\n<p>    # \u6c42\u6700\u4f4e\u5de5\u8d44<\/p>\n<p>    min_salary = data[&#39;Salary&#39;].min()<\/p>\n<p>    print(f&quot;\u6700\u4f4e\u5de5\u8d44\u662f: {min_salary}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801: {response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6570\u636e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5de5\u8d44\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6216Seaborn\u5e93\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u5de5\u8d44\u5206\u5e03\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u5de5\u8d44\u5206\u5e03\u56fe<\/strong><\/h2>\n<p>sns.histplot(data[&#39;Salary&#39;], kde=True)<\/p>\n<p>plt.title(&#39;\u5de5\u8d44\u5206\u5e03\u56fe&#39;)<\/p>\n<p>plt.xlabel(&#39;\u5de5\u8d44&#39;)<\/p>\n<p>plt.ylabel(&#39;\u9891\u6570&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u6c42\u6700\u4f4e\u5de5\u8d44\u3002\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u8bfb\u53d6\u6570\u636e\u3001\u6570\u636e\u5904\u7406\u3001\u8ba1\u7b97\u6700\u4f4e\u5de5\u8d44\u4ee5\u53ca\u6570\u636e\u53ef\u89c6\u5316\u3002<strong>\u6570\u636e\u6e05\u6d17\u548c\u5904\u7406\u662f\u5173\u952e\u6b65\u9aa4\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u6b63\u786e\u6027<\/strong>\u3002\u6b64\u5916\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u4ece\u6570\u636e\u5e93\u548cAPI\u83b7\u53d6\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Matplotlib\u548cSeaborn\u5e93\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002Python\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u80fd\u529b\u4f7f\u5176\u6210\u4e3a\u6c42\u6700\u4f4e\u5de5\u8d44\u7684\u7406\u60f3\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u6700\u4f4e\u5de5\u8d44\u7684\u5177\u4f53\u6b65\u9aa4\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8ba1\u7b97\u6700\u4f4e\u5de5\u8d44\u901a\u5e38\u5305\u62ec\u8f93\u5165\u6bcf\u5c0f\u65f6\u5de5\u8d44\u548c\u5de5\u4f5c\u5c0f\u65f6\u6570\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u76f8\u4e58\u4ee5\u5f97\u5230\u603b\u5de5\u8d44\u3002\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u8f93\u5165\u51fd\u6570\u6536\u96c6\u6570\u636e\uff0c\u5e76\u8fd0\u7528\u6570\u5b66\u8fd0\u7b97\u8fdb\u884c\u8ba1\u7b97\u3002\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u5de5\u8d44\u4e0d\u4f4e\u4e8e\u6cd5\u5b9a\u6700\u4f4e\u5de5\u8d44\u6807\u51c6\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u5de5\u8d44\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982Pandas\u5e93\u6765\u5904\u7406\u548c\u5206\u6790\u5de5\u8d44\u6570\u636e\u3002Pandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u5927\u578b\u5de5\u8d44\u6570\u636e\u96c6\uff0c\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u8ba1\u7b97\u548c\u53ef\u89c6\u5316\u7b49\u64cd\u4f5c\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u548c\u7ba1\u7406\u5de5\u8d44\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u6700\u4f4e\u5de5\u8d44\u6807\u51c6\uff1f<\/strong><br \/>\u5728Python\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5e38\u91cf\u6765\u8868\u793a\u6700\u4f4e\u5de5\u8d44\u6807\u51c6\u3002\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\uff0c\u5982\u679c\u8ba1\u7b97\u5f97\u51fa\u7684\u5de5\u8d44\u4f4e\u4e8e\u6b64\u6807\u51c6\uff0c\u53ef\u4ee5\u629b\u51fa\u8b66\u544a\u6216\u63d0\u793a\uff0c\u786e\u4fdd\u6240\u6709\u8ba1\u7b97\u7684\u5de5\u8d44\u90fd\u7b26\u5408\u6700\u4f4e\u5de5\u8d44\u8981\u6c42\u3002\u8fd9\u79cd\u65b9\u5f0f\u6709\u52a9\u4e8e\u7ef4\u62a4\u7a0b\u5e8f\u7684\u5408\u89c4\u6027\u5e76\u4fdd\u62a4\u5458\u5de5\u6743\u76ca\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u5982\u4f55\u6c42\u6700\u4f4e\u5de5\u8d44\uff1f \u5229\u7528Python\u6765\u8ba1\u7b97\u6700\u4f4e\u5de5\u8d44\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u8bfb\u53d6\u5de5\u8d44\u6570\u636e\u6587\u4ef6\u3001\u4ece\u6570\u636e\u5e93\u4e2d\u63d0 [&hellip;]","protected":false},"author":3,"featured_media":1030858,"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\/1030846"}],"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=1030846"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030846\/revisions"}],"predecessor-version":[{"id":1030860,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030846\/revisions\/1030860"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1030858"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1030846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1030846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1030846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}