{"id":1058739,"date":"2024-12-31T15:19:00","date_gmt":"2024-12-31T07:19:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058739.html"},"modified":"2024-12-31T15:19:02","modified_gmt":"2024-12-31T07:19:02","slug":"python%e5%a6%82%e4%bd%95%e5%89%94%e9%99%a4%e8%a1%a8%e4%b8%ad%e9%87%8d%e5%a4%8d%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058739.html","title":{"rendered":"python\u5982\u4f55\u5254\u9664\u8868\u4e2d\u91cd\u590d\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/8b9137ab-5c35-450c-a3fc-24d8a60b82e8.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5254\u9664\u8868\u4e2d\u91cd\u590d\u7684\u503c\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5254\u9664\u8868\u4e2d\u91cd\u590d\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982<strong>\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u4f7f\u7528\u5b57\u5178\uff08dictionary\uff09\u3001\u4f7f\u7528Pandas\u5e93\u7684drop_duplicates\u65b9\u6cd5<\/strong>\u7b49\u3002\u6700\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Pandas\u5e93\u7684drop_duplicates\u65b9\u6cd5\uff0c\u56e0\u4e3aPandas\u5e93\u4e13\u95e8\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u529f\u80fd\u3002<strong>Pandas\u5e93\u4e0d\u4ec5\u80fd\u53bb\u9664\u91cd\u590d\u503c\uff0c\u8fd8\u80fd\u4fdd\u7559\u7279\u5b9a\u5217\u7684\u552f\u4e00\u503c\uff0c\u5141\u8bb8\u81ea\u5b9a\u4e49\u91cd\u590d\u5224\u65ad\u6807\u51c6<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Pandas\u5e93\u7684drop_duplicates\u65b9\u6cd5\u5254\u9664\u8868\u4e2d\u91cd\u590d\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5e76\u5bfc\u5165Pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165Pandas\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">!pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bfc\u5165Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846\uff0c\u5305\u542b\u4e00\u4e9b\u91cd\u590d\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Alice&#39;, &#39;David&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 25, 35, 30],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;New York&#39;, &#39;Chicago&#39;, &#39;Los Angeles&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;Original DataFrame:&quot;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Original DataFrame:<\/p>\n<p>    Name  Age         City<\/p>\n<p>0  Alice   25     New York<\/p>\n<p>1    Bob   30  Los Angeles<\/p>\n<p>2  Alice   25     New York<\/p>\n<p>3  David   35      Chicago<\/p>\n<p>4    Bob   30  Los Angeles<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528drop_duplicates\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u53bb\u9664\u6240\u6709\u5217\u7684\u91cd\u590d\u503c<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u8981\u53bb\u9664\u6240\u6709\u5217\u7684\u91cd\u590d\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528drop_duplicates\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_unique = df.drop_duplicates()<\/p>\n<p>print(&quot;\\nDataFrame with duplicates removed:&quot;)<\/p>\n<p>print(df_unique)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>DataFrame with duplicates removed:<\/p>\n<p>    Name  Age         City<\/p>\n<p>0  Alice   25     New York<\/p>\n<p>1    Bob   30  Los Angeles<\/p>\n<p>3  David   35      Chicago<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u53bb\u9664\u7279\u5b9a\u5217\u7684\u91cd\u590d\u503c<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u60f3\u53bb\u9664\u7279\u5b9a\u5217\u7684\u91cd\u590d\u503c\uff0c\u4f8b\u5982Name\u5217\uff0c\u53ef\u4ee5\u6307\u5b9asubset\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_unique_name = df.drop_duplicates(subset=[&#39;Name&#39;])<\/p>\n<p>print(&quot;\\nDataFrame with duplicates removed based on &#39;Name&#39; column:&quot;)<\/p>\n<p>print(df_unique_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>DataFrame with duplicates removed based on &#39;Name&#39; column:<\/p>\n<p>    Name  Age         City<\/p>\n<p>0  Alice   25     New York<\/p>\n<p>1    Bob   30  Los Angeles<\/p>\n<p>3  David   35      Chicago<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4fdd\u7559\u6700\u540e\u51fa\u73b0\u7684\u91cd\u590d\u503c<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cdrop_duplicates\u65b9\u6cd5\u4fdd\u7559\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u503c\u3002\u5982\u679c\u8981\u4fdd\u7559\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u503c\uff0c\u53ef\u4ee5\u8bbe\u7f6ekeep\u53c2\u6570\u4e3a&#39;last&#39;\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_unique_last = df.drop_duplicates(keep=&#39;last&#39;)<\/p>\n<p>print(&quot;\\nDataFrame with duplicates removed, keeping the last occurrence:&quot;)<\/p>\n<p>print(df_unique_last)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>DataFrame with duplicates removed, keeping the last occurrence:<\/p>\n<p>    Name  Age         City<\/p>\n<p>2  Alice   25     New York<\/p>\n<p>3  David   35      Chicago<\/p>\n<p>4    Bob   30  Los Angeles<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u53bb\u9664\u91cd\u590d\u503c\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u96c6\u5408\uff08set\uff09<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6570\u636e\u8f83\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u53bb\u9664\u91cd\u590d\u503c\u3002\u96c6\u5408\u662f\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u5bb9\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_data = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_data = list(set(list_data))<\/p>\n<p>print(&quot;\\nList with duplicates removed using set:&quot;)<\/p>\n<p>print(unique_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>List with duplicates removed using set:<\/p>\n<p>[1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b57\u5178\uff08dictionary\uff09<\/h4>\n<\/p>\n<p><p>\u4ecePython 3.7\u5f00\u59cb\uff0c\u5b57\u5178\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u53bb\u9664\u91cd\u590d\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_data = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_data = list(dict.fromkeys(list_data))<\/p>\n<p>print(&quot;\\nList with duplicates removed using dictionary:&quot;)<\/p>\n<p>print(unique_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>List with duplicates removed using dictionary:<\/p>\n<p>[1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5254\u9664\u8868\u4e2d\u91cd\u590d\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u96c6\u5408\u3001\u5b57\u5178\u548cPandas\u5e93\u3002\u5176\u4e2d\uff0c<strong>Pandas\u5e93\u7684drop_duplicates\u65b9\u6cd5\u6700\u4e3a\u7075\u6d3b\u548c\u5f3a\u5927\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u6846<\/strong>\u3002\u901a\u8fc7\u6307\u5b9a\u4e0d\u540c\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u53bb\u9664\u6240\u6709\u5217\u6216\u7279\u5b9a\u5217\u7684\u91cd\u590d\u503c\uff0c\u5e76\u9009\u62e9\u4fdd\u7559\u7b2c\u4e00\u6b21\u6216\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u503c\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5254\u9664\u91cd\u590d\u503c\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u552f\u4e00\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u627e\u5230\u8868\u4e2d\u7684\u91cd\u590d\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u8f7b\u677e\u627e\u5230\u8868\u4e2d\u7684\u91cd\u590d\u503c\u3002\u901a\u8fc7\u8c03\u7528<code>DataFrame.duplicated()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u68c0\u67e5\u6bcf\u4e00\u884c\u662f\u5426\u662f\u91cd\u590d\u7684\uff0c\u5e76\u6839\u636e\u9700\u8981\u9009\u62e9\u663e\u793a\u6216\u5220\u9664\u8fd9\u4e9b\u884c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5141\u8bb8\u4f60\u8bbe\u7f6e\u53c2\u6570\u6765\u5b9a\u4e49\u4ec0\u4e48\u6837\u7684\u884c\u88ab\u8ba4\u4e3a\u662f\u91cd\u590d\u7684\uff0c\u4f8b\u5982\u53ea\u8003\u8651\u67d0\u4e9b\u5217\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5220\u9664\u91cd\u590d\u503c\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4f7f\u7528Pandas\u5e93\u7684<code>drop_duplicates()<\/code>\u65b9\u6cd5\u662f\u5220\u9664\u91cd\u590d\u503c\u7684\u6700\u4f73\u9009\u62e9\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5217\u53bb\u9664\u91cd\u590d\u884c\uff0c\u5e76\u63d0\u4f9b\u591a\u4e2a\u53c2\u6570\u6765\u63a7\u5236\u5220\u9664\u7684\u884c\u4e3a\uff0c\u6bd4\u5982\u4fdd\u7559\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u503c\u6216\u6700\u540e\u4e00\u4e2a\u51fa\u73b0\u7684\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u6574\u6d01\u6027\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Python\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u5982\u4f55\u6709\u6548\u5730\u5254\u9664\u91cd\u590d\u503c\uff1f<\/strong><br \/>\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Pandas\u5e93\u4e2d\u7684<code>drop_duplicates()<\/code>\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408<code>inplace=True<\/code>\u53c2\u6570\u6765\u76f4\u63a5\u5728\u539f\u59cb\u6570\u636e\u4e0a\u8fdb\u884c\u64cd\u4f5c\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u62f7\u8d1d\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>subset<\/code>\u53c2\u6570\u53ef\u4ee5\u4ec5\u5173\u6ce8\u7279\u5b9a\u5217\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u5728\u5254\u9664\u91cd\u590d\u503c\u4e4b\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u9002\u5f53\u7684\u9884\u5904\u7406\uff0c\u4ee5\u907f\u514d\u56e0\u6570\u636e\u7c7b\u578b\u4e0d\u4e00\u81f4\u800c\u5f15\u53d1\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5254\u9664\u8868\u4e2d\u91cd\u590d\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u4f7f\u7528\u5b57\u5178\uff08dictionary\uff09\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1058742,"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\/1058739"}],"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=1058739"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058739\/revisions"}],"predecessor-version":[{"id":1058743,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058739\/revisions\/1058743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1058742"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1058739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1058739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1058739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}