{"id":1067349,"date":"2024-12-31T16:33:59","date_gmt":"2024-12-31T08:33:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1067349.html"},"modified":"2024-12-31T16:34:01","modified_gmt":"2024-12-31T08:34:01","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e8%a1%a8%e6%a0%bc%e6%9f%90%e4%b8%80%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1067349.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u8868\u683c\u67d0\u4e00\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/d1fc9305-895a-4367-9917-7f656f365fa7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5220\u9664\u8868\u683c\u67d0\u4e00\u884c\" \/><\/p>\n<p><p> <strong>\u8981\u5220\u9664\u8868\u683c\u4e2d\u7684\u67d0\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u6216csv\u6a21\u5757\u3002\u5177\u4f53\u65b9\u6cd5\u5982\u4e0b\uff1a\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528openpyxl\u5e93\u3001\u4f7f\u7528csv\u6a21\u5757\u3002<\/strong> \u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u6765\u5220\u9664\u8868\u683c\u4e2d\u7684\u67d0\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c\u6570\u636e\u5de5\u7a0b\u9886\u57df\u3002\u4f7f\u7528pandas\u5220\u9664\u8868\u683c\u4e2d\u7684\u67d0\u4e00\u884c\u975e\u5e38\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5pandas\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5220\u9664\u67d0\u4e00\u884c<\/h4>\n<\/p>\n<p><p>\u8fd9\u91cc\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2aCSV\u6587\u4ef6<code>data.csv<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>name,age,city<\/p>\n<p>Alice,30,New York<\/p>\n<p>Bob,25,Los Angeles<\/p>\n<p>Charlie,35,Chicago<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u60f3\u8981\u5220\u9664\u5e74\u9f84\u4e3a30\u7684\u90a3\u4e00\u884c\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>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u5220\u9664\u6307\u5b9a\u884c<\/strong><\/h2>\n<p>df = df[df[&#39;age&#39;] != 30]<\/p>\n<h2><strong>\u5c06\u7ed3\u679c\u4fdd\u5b58\u56deCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;data.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528openpyxl\u5e93<\/h3>\n<\/p>\n<p><p>openpyxl\u5e93\u662f\u4e00\u4e2a\u53ef\u4ee5\u8bfb\u5199Excel 2010 xlsx\/xlsm\/xltx\/xltm\u6587\u4ef6\u7684Python\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5openpyxl\u5e93<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5openpyxl\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5220\u9664\u67d0\u4e00\u884c<\/h4>\n<\/p>\n<p><p>\u8fd9\u91cc\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2aExcel\u6587\u4ef6<code>data.xlsx<\/code>\uff0c\u5185\u5bb9\u4e0e\u4e0a\u9762\u7684CSV\u6587\u4ef6\u7c7b\u4f3c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import load_workbook<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>wb = load_workbook(&#39;data.xlsx&#39;)<\/p>\n<p>ws = wb.active<\/p>\n<h2><strong>\u904d\u5386\u6240\u6709\u884c\uff0c\u627e\u5230\u8981\u5220\u9664\u7684\u884c<\/strong><\/h2>\n<p>for row in ws.iter_rows():<\/p>\n<p>    if row[1].value == 30:  # \u5047\u8bbe\u8981\u5220\u9664\u7b2c\u4e8c\u5217\u503c\u4e3a30\u7684\u884c<\/p>\n<p>        ws.delete_rows(row[0].row)<\/p>\n<h2><strong>\u4fdd\u5b58\u4fee\u6539\u540e\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;data.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528csv\u6a21\u5757<\/h3>\n<\/p>\n<p><p>csv\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\uff0c\u7528\u4e8e\u8bfb\u5199CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u5220\u9664\u67d0\u4e00\u884c<\/h4>\n<\/p>\n<p><p>\u8fd9\u91cc\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2aCSV\u6587\u4ef6<code>data.csv<\/code>\uff0c\u5185\u5bb9\u4e0e\u4e0a\u9762\u7684CSV\u6587\u4ef6\u7c7b\u4f3c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>rows = []<\/p>\n<p>with open(&#39;data.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    header = next(reader)<\/p>\n<p>    for row in reader:<\/p>\n<p>        if row[1] != &#39;30&#39;:  # \u5047\u8bbe\u8981\u5220\u9664\u7b2c\u4e8c\u5217\u503c\u4e3a30\u7684\u884c<\/p>\n<p>            rows.append(row)<\/p>\n<h2><strong>\u5c06\u7ed3\u679c\u4fdd\u5b58\u56deCSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow(header)<\/p>\n<p>    writer.writerows(rows)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u5c31\u662f\u5220\u9664\u8868\u683c\u4e2d\u67d0\u4e00\u884c\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u3002<strong>\u4f7f\u7528pandas\u5e93\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\uff1b\u4f7f\u7528openpyxl\u5e93\u53ef\u4ee5\u5904\u7406Excel\u6587\u4ef6\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9Excel\u6587\u4ef6\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\uff1b\u4f7f\u7528csv\u6a21\u5757\u53ef\u4ee5\u5904\u7406\u7b80\u5355\u7684CSV\u6587\u4ef6\uff0c\u9002\u7528\u4e8e\u4e0d\u9700\u8981\u4f9d\u8d56\u5916\u90e8\u5e93\u7684\u573a\u666f\u3002<\/strong> \u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u8868\u683c\u7684\u7279\u5b9a\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5220\u9664\u8868\u683c\u7684\u7279\u5b9a\u884c\uff0c\u901a\u5e38\u4f7f\u7528pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u3002\u53ef\u4ee5\u901a\u8fc7<code>drop()<\/code>\u65b9\u6cd5\u6307\u5b9a\u8981\u5220\u9664\u7684\u884c\u7d22\u5f15\u6216\u6761\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u9009\u62e9\u662f\u5426\u5728\u539f\u59cb\u6570\u636e\u4e0a\u8fdb\u884c\u4fee\u6539\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame\ndata = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [25, 30, 35]}\ndf = pd.DataFrame(data)\n\n# \u5220\u9664\u7d22\u5f15\u4e3a1\u7684\u884c\ndf_dropped = df.drop(index=1)\nprint(df_dropped)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7075\u6d3b\u5730\u5220\u9664\u591a\u884c\u6216\u6839\u636e\u6761\u4ef6\u5220\u9664\u884c\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5220\u9664\u8868\u683c\u884c\u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u5220\u9664\u884c\u65f6\uff0c\u786e\u4fdd\u4e86\u89e3\u6570\u636e\u7ed3\u6784\u53ca\u5176\u7d22\u5f15\u3002\u82e5\u4f7f\u7528<code>drop()<\/code>\u65b9\u6cd5\uff0c\u9ed8\u8ba4\u4e0d\u4f1a\u4fee\u6539\u539f\u59cbDataFrame\uff0c\u9664\u975e\u8bbe\u7f6e<code>inplace=True<\/code>\u3002\u6b64\u5916\uff0c\u5728\u5220\u9664\u6570\u636e\u540e\uff0c\u53ef\u80fd\u9700\u8981\u91cd\u7f6e\u7d22\u5f15\uff0c\u4ee5\u907f\u514d\u540e\u7eed\u64cd\u4f5c\u65f6\u4ea7\u751f\u6df7\u6dc6\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7528\u6765\u5220\u9664\u8868\u683c\u884c\uff1f<\/strong><br \/>\u9664\u4e86pandas\uff0cPython\u7684\u5176\u4ed6\u6570\u636e\u5904\u7406\u5e93\u5982openpyxl\u548ccsv\u4e5f\u80fd\u5b9e\u73b0\u5220\u9664\u8868\u683c\u884c\u7684\u529f\u80fd\u3002openpyxl\u9002\u7528\u4e8e\u64cd\u4f5cExcel\u6587\u4ef6\uff0c\u800ccsv\u5e93\u5219\u9002\u5408\u5904\u7406CSV\u683c\u5f0f\u7684\u6570\u636e\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u65f6\uff0c\u9700\u8981\u5148\u8bfb\u53d6\u6587\u4ef6\uff0c\u8fdb\u884c\u884c\u7684\u5220\u9664\uff0c\u518d\u4fdd\u5b58\u6587\u4ef6\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u636e\u5904\u7406\uff0cpandas\u901a\u5e38\u662f\u66f4\u9ad8\u6548\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5220\u9664\u8868\u683c\u4e2d\u7684\u67d0\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u6216csv\u6a21\u5757\u3002\u5177\u4f53\u65b9\u6cd5\u5982\u4e0b\uff1a [&hellip;]","protected":false},"author":3,"featured_media":1067356,"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\/1067349"}],"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=1067349"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067349\/revisions"}],"predecessor-version":[{"id":1067362,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067349\/revisions\/1067362"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1067356"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1067349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1067349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1067349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}