{"id":1032336,"date":"2024-12-31T11:32:47","date_gmt":"2024-12-31T03:32:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032336.html"},"modified":"2024-12-31T11:32:49","modified_gmt":"2024-12-31T03:32:49","slug":"python%e6%95%b0%e6%8d%ae%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e7%ac%ac%e4%b8%80%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1032336.html","title":{"rendered":"python\u6570\u636e\u5982\u4f55\u63d0\u53d6\u7b2c\u4e00\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/7359c9b4-518d-468f-8606-e506e017b27c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u6570\u636e\u5982\u4f55\u63d0\u53d6\u7b2c\u4e00\u5217\" \/><\/p>\n<p><p> <strong>\u63d0\u53d6Python\u6570\u636e\u7b2c\u4e00\u5217\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4f7f\u7528pandas\u3001numpy\u3001csv\u6a21\u5757\u7b49\u3002\u63a8\u8350\u4f7f\u7528pandas\u6a21\u5757\u3001\u56e0\u4e3a\u529f\u80fd\u5f3a\u5927\u3001\u4f7f\u7528\u65b9\u4fbf\u3001\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u6570\u636e\u8bfb\u5199\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdpandas\u6a21\u5757\u5982\u4f55\u63d0\u53d6\u6570\u636e\u7684\u7b2c\u4e00\u5217\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u63d0\u53d6\u6570\u636e\u7684\u67d0\u4e00\u5217\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u4eceCSV\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\uff0c\u8fd8\u662f\u4eceExcel\u6587\u4ef6\u4e2d\u63d0\u53d6\u6570\u636e\uff0cpandas\u90fd\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u5b8c\u6210\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u63d0\u53d6\u7b2c\u4e00\u5217<\/p>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u4fbf\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5pandas\u5e93\uff0c\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e00\u5217<\/strong><\/h2>\n<p>first_column = df.iloc[:, 0]<\/p>\n<p>print(first_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pd.read_csv<\/code>\u65b9\u6cd5\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728DataFrame\u5bf9\u8c61\u4e2d\u3002\u7136\u540e\uff0c\u901a\u8fc7<code>iloc<\/code>\u65b9\u6cd5\u63d0\u53d6\u7b2c\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e00\u5217<\/strong><\/h2>\n<p>first_column = df.iloc[:, 0]<\/p>\n<p>print(first_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7c7b\u4f3c\u5730\uff0c\u4f7f\u7528<code>pd.read_excel<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u5e76\u63d0\u53d6\u7b2c\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528numpy\u63d0\u53d6\u7b2c\u4e00\u5217<\/p>\n<\/p>\n<p><p>numpy\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528numpy\u6765\u63d0\u53d6\u6570\u636e\u7684\u7b2c\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2anumpy\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e00\u5217<\/strong><\/h2>\n<p>first_column = data[:, 0]<\/p>\n<p>print(first_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2anumpy\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u63d0\u53d6\u7b2c\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528csv\u6a21\u5757\u63d0\u53d6\u7b2c\u4e00\u5217<\/p>\n<\/p>\n<p><p>csv\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6\u3002\u867d\u7136\u529f\u80fd\u6ca1\u6709pandas\u548cnumpy\u5f3a\u5927\uff0c\u4f46\u5bf9\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u64cd\u4f5c\uff0ccsv\u6a21\u5757\u4e5f\u8db3\u591f\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6253\u5f00CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    # \u63d0\u53d6\u7b2c\u4e00\u5217<\/p>\n<p>    first_column = [row[0] for row in reader]<\/p>\n<p>    print(first_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528csv.reader\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u53d6\u7b2c\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8be6\u7ec6\u63cf\u8ff0pandas\u6a21\u5757\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p><strong>pandas\u6a21\u5757\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u5229\u5668\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u64cd\u4f5c\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/p>\n<p>pandas\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u6570\u636e\u8bfb\u53d6\uff0c\u5305\u62ecCSV\u3001Excel\u3001SQL\u7b49\u3002\u901a\u8fc7<code>pd.read_csv<\/code>\u3001<code>pd.read_excel<\/code>\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u8bfb\u53d6\u4e3aDataFrame\u5bf9\u8c61\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u636e\u7b5b\u9009<\/strong><\/p>\n<p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u7b5b\u9009\u3002pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u7b5b\u9009\u65b9\u6cd5\uff0c\u5305\u62ec\u6309\u6761\u4ef6\u7b5b\u9009\u3001\u6309\u5217\u7b5b\u9009\u7b49\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u7b5b\u9009\u6570\u636e<\/p>\n<p>filtered_data = df[df[&#39;column_name&#39;] &gt; value]<\/p>\n<h2><strong>\u63d0\u53d6\u6307\u5b9a\u5217<\/strong><\/h2>\n<p>selected_columns = df[[&#39;column1&#39;, &#39;column2&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6570\u636e\u6e05\u6d17<\/strong><\/p>\n<p>\u6570\u636e\u6e05\u6d17\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u91cd\u8981\u6b65\u9aa4\uff0cpandas\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u4fbf\u7684\u6570\u636e\u6e05\u6d17\u65b9\u6cd5\uff0c\u5305\u62ec\u5904\u7406\u7f3a\u5931\u503c\u3001\u5220\u9664\u91cd\u590d\u503c\u7b49\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u5904\u7406\u7f3a\u5931\u503c<\/p>\n<p>df = df.dropna()<\/p>\n<h2><strong>\u5220\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>df = df.drop_duplicates()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u6570\u636e\u7edf\u8ba1<\/strong><\/p>\n<p>pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u7edf\u8ba1\u65b9\u6cd5\uff0c\u5305\u62ec\u63cf\u8ff0\u6027\u7edf\u8ba1\u3001\u5206\u7ec4\u7edf\u8ba1\u7b49\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u63cf\u8ff0\u6027\u7edf\u8ba1<\/p>\n<p>summary = df.describe()<\/p>\n<h2><strong>\u5206\u7ec4\u7edf\u8ba1<\/strong><\/h2>\n<p>grouped_data = df.groupby(&#39;column_name&#39;).mean()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u6570\u636e\u53ef\u89c6\u5316<\/strong><\/p>\n<p>pandas\u53ef\u4ee5\u4e0ematplotlib\u7b49\u53ef\u89c6\u5316\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>df.plot(x=&#39;column_x&#39;, y=&#39;column_y&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528pandas\u8fdb\u884c\u6570\u636e\u8bfb\u53d6\u3001\u7b5b\u9009\u3001\u6e05\u6d17\u3001\u7edf\u8ba1\u548c\u53ef\u89c6\u5316\uff0c\u4e3a\u6570\u636e\u5206\u6790\u63d0\u4f9b\u5f3a\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u6570\u636e\u7684\u7b2c\u4e00\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\u63d0\u53d6\u6570\u636e\u7684\u7b2c\u4e00\u5217\uff0c\u4f8b\u5982\u4f7f\u7528Pandas\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Pandas\u5e93\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u6570\u636e\u5e76\u63d0\u53d6\u7b2c\u4e00\u5217\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u52a0\u8f7d\u6570\u636e\u6587\u4ef6\ndata = pd.read_csv(&#39;your_file.csv&#39;)\n\n# \u63d0\u53d6\u7b2c\u4e00\u5217\nfirst_column = data.iloc[:, 0]\nprint(first_column)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8eCSV\u6587\u4ef6\uff0cPandas\u4f1a\u81ea\u52a8\u5904\u7406\u6570\u636e\u683c\u5f0f\u3002<\/p>\n<p><strong>\u4f7f\u7528NumPy\u63d0\u53d6\u6570\u636e\u7684\u7b2c\u4e00\u5217\u6709\u4ec0\u4e48\u65b9\u6cd5\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u7684\u6570\u636e\u4ee5NumPy\u6570\u7ec4\u683c\u5f0f\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u8f7b\u677e\u63d0\u53d6\u7b2c\u4e00\u5217\u3002\u4ee5\u4e0b\u662f\u76f8\u5173\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4\ndata = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n\n# \u63d0\u53d6\u7b2c\u4e00\u5217\nfirst_column = data[:, 0]\nprint(first_column)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5904\u7406\u6570\u503c\u578b\u6570\u636e\uff0c\u6267\u884c\u6548\u7387\u8f83\u9ad8\u3002<\/p>\n<p><strong>\u5728\u63d0\u53d6\u7b2c\u4e00\u5217\u65f6\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5904\u7406\u7f3a\u5931\u503c\u7684\u7b56\u7565\u5728\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u91cd\u8981\u3002\u5982\u679c\u4f60\u4f7f\u7528Pandas\u63d0\u53d6\u7b2c\u4e00\u5217\uff0c\u53ef\u4ee5\u5728\u63d0\u53d6\u4e4b\u524d\u6216\u4e4b\u540e\u4f7f\u7528<code>dropna()<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = pd.read_csv(&#39;your_file.csv&#39;)\n\n# \u63d0\u53d6\u7b2c\u4e00\u5217\u5e76\u4e22\u5f03\u7f3a\u5931\u503c\nfirst_column = data.iloc[:, 0].dropna()\nprint(first_column)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u63d0\u53d6\u7684\u7b2c\u4e00\u5217\u4e0d\u5305\u542b\u4efb\u4f55\u7f3a\u5931\u503c\uff0c\u6709\u52a9\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u63d0\u53d6Python\u6570\u636e\u7b2c\u4e00\u5217\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4f7f\u7528pandas\u3001numpy\u3001csv\u6a21\u5757\u7b49\u3002\u63a8\u8350\u4f7f\u7528panda [&hellip;]","protected":false},"author":3,"featured_media":1032342,"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\/1032336"}],"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=1032336"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032336\/revisions"}],"predecessor-version":[{"id":1032346,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032336\/revisions\/1032346"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1032342"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1032336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1032336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1032336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}