{"id":175916,"date":"2024-05-08T19:00:25","date_gmt":"2024-05-08T11:00:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/175916.html"},"modified":"2024-05-08T19:00:37","modified_gmt":"2024-05-08T11:00:37","slug":"python-%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4dataframe%e6%9f%90%e5%88%97%e5%85%83%e7%b4%a0%e4%b8%ba0%e7%9a%84%e8%a1%8c%ef%bc%8c%e4%b8%8d%e7%94%a8isin","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/175916.html","title":{"rendered":"python \u5982\u4f55\u5220\u9664dataframe\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\uff0c\u4e0d\u7528isin"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27054030\/a3a3eac2-9777-4fd6-bf52-fdffeff90274.webp\" alt=\"python \u5982\u4f55\u5220\u9664dataframe\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\uff0c\u4e0d\u7528isin\" \/><\/p>\n<p><p>\u5220\u9664DataFrame\u4e2d\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>~<\/code>\u64cd\u4f5c\u7b26\u4ee5\u53ca<code>loc<\/code>\u6216\u8005<code>query<\/code>\u65b9\u6cd5\u8fdb\u884c\u9009\u62e9\u6027\u8fc7\u6ee4\u64cd\u4f5c\u3002<strong>\u6838\u5fc3\u7684\u64cd\u4f5c\u5305\u62ec\uff1a\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08!=\uff09\u3001\u4f7f\u7528<code>loc<\/code>\u65b9\u6cd5\u7b5b\u9009\u3001\u6216\u4f7f\u7528<code>query<\/code>\u65b9\u6cd5\u3002<\/strong> \u8fd9\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u6709\u6548\u5730\u79fb\u9664\u542b\u6709\u96f6\u503c\u7684\u884c\uff0c\u800c\u4e0d\u6539\u53d8\u539f\u59cbDataFrame\u7684\u5176\u4ed6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u9488\u5bf9\u7279\u5b9a\u5217\u5e94\u7528\u4e0d\u7b49\u4e8e\uff08!=\uff09\u8fd0\u7b97\u7b26\u4e0e0\u6bd4\u8f83\uff0c\u7136\u540e\u4f7f\u7528<code>loc<\/code>\u65b9\u6cd5\u5c06\u7ed3\u679c\u4e3aTrue\u7684\u884c\u9009\u51fa\u6765\u3002<code>query<\/code>\u65b9\u6cd5\u5219\u662f\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u7b5b\u9009\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u4f60\u4f7f\u7528\u50cf\u5b57\u7b26\u4e32\u4e00\u6837\u7684\u8868\u8fbe\u5f0f\u8fdb\u884c\u7b5b\u9009\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528LOC\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><strong>\u901a\u8fc7<code>loc<\/code>\u65b9\u6cd5<\/strong>\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5e03\u5c14\u6761\u4ef6\u6765\u7b5b\u9009\u6570\u636e\u3002\u9996\u5148\uff0c\u4f60\u4f1a\u751f\u6210\u4e00\u4e2a\u5e03\u5c14\u5e8f\u5217\uff0c\u8868\u793a\u67d0\u5217\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4e0d\u7b49\u4e8e0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbedf\u662f\u4e00\u4e2a\u5df2\u5b58\u5728\u7684DataFrame<\/strong><\/h2>\n<h2><strong>&#039;column_name&#039; \u662f\u9700\u8981\u68c0\u67e5\u7684\u5217\u540d\u79f0<\/strong><\/h2>\n<h2><strong>\u751f\u6210\u6761\u4ef6\u7b5b\u9009\u5e03\u5c14\u5e8f\u5217<\/strong><\/h2>\n<p>not_zero = df[&#039;column_name&#039;] != 0<\/p>\n<h2><strong>\u5e94\u7528\u6761\u4ef6\u7b5b\u9009<\/strong><\/h2>\n<p>filtered_df = df.loc[not_zero]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528<code>loc<\/code>\u65b9\u6cd5\u65f6\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u6d89\u53ca\u5e03\u5c14\u7d22\u5f15\u3002\u5e03\u5c14\u7d22\u5f15\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u4f60\u6839\u636e\u5217\u4e2d\u7684\u503c\u5bf9\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528QUERY\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>query<\/code>\u65b9\u6cd5<\/strong>\u53ef\u4ee5\u76f4\u63a5\u5728\u51fd\u6570\u5185\u5199\u5165\u7b5b\u9009\u6761\u4ef6\u7684\u8868\u8fbe\u5f0f\uff0c\u4e3a\u6570\u636e\u7b5b\u9009\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbedf\u662f\u4e00\u4e2a\u5df2\u5b58\u5728\u7684DataFrame<\/strong><\/h2>\n<h2><strong>&#039;column_name&#039; \u662f\u9700\u8981\u68c0\u67e5\u7684\u5217\u540d\u79f0<\/strong><\/h2>\n<p>filtered_df = df.query(&#039;column_name != 0&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>query<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7b80\u5316\u7b5b\u9009\u8bed\u53e5\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u61c2\u3002\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u4f20\u8fbe\u51fa\u7b5b\u9009\u7684\u610f\u56fe\uff0c\u800c\u65e0\u9700\u5148\u5355\u72ec\u521b\u5efa\u5e03\u5c14\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u907f\u514d\u4f7f\u7528ISIN\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u901a\u5e38<code>isin<\/code>\u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u67d0\u4e2a\u5217\u7684\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u4e00\u4e2a\u6307\u5b9a\u7684\u96c6\u5408\u4e2d\uff0c\u5982\u679c\u76ee\u7684\u662f\u6392\u9664\u6240\u6709\u503c\u4e3a0\u7684\u884c\uff0c\u5219\u4e0d\u9700\u8981\u7528\u5230<code>isin<\/code>\u65b9\u6cd5\uff0c\u6b63\u5982\u4e0a\u8ff0\u63cf\u8ff0\u7684\u4f7f\u7528<code>loc<\/code>\u6216<code>query<\/code>\u65b9\u6cd5\u5373\u53ef\u5b9e\u73b0\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9644\u52a0\u64cd\u4f5c\uff1a\u91cd\u7f6e\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5728\u8fc7\u6ee4\u6570\u636e\u4e4b\u540e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7d22\u5f15\u7684\u4e0d\u8fde\u7eed\uff0c\u53ef\u4ee5\u9009\u62e9\u91cd\u7f6e\u7d22\u5f15\u6765\u4fdd\u6301\u6570\u636e\u7684\u6574\u6d01\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filtered_df = filtered_df.reset_index(drop=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6dfb\u52a0<code>drop=True<\/code>\u53c2\u6570\u53ef\u4ee5\u9632\u6b62\u65e7\u7d22\u5f15\u6dfb\u52a0\u4e3aDataFrame\u7684\u4e00\u5217\uff0c\u7b80\u5316\u6700\u7ec8\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u793a\u4f8b\u4ee3\u7801\u6c47\u603b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5220\u9664\u542b\u6709\u5217\u5143\u7d20\u503c\u4e3a0\u7684\u884c\u7684\u5b8c\u6574\u8fc7\u7a0b\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 = {&#039;column_name&#039;: [1, 0, 2, 0, 3, 4], &#039;other_column&#039;: [5, 6, 7, 8, 9, 0]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528LOC\u65b9\u6cd5\u5220\u9664\u542b0\u5143\u7d20\u7684\u884c<\/strong><\/h2>\n<p>filtered_df_loc = df.loc[df[&#039;column_name&#039;] != 0]<\/p>\n<h2><strong>\u4f7f\u7528QUERY\u65b9\u6cd5\u5220\u9664\u542b0\u5143\u7d20\u7684\u884c<\/strong><\/h2>\n<p>filtered_df_query = df.query(&#039;column_name != 0&#039;)<\/p>\n<h2><strong>\u9009\u62e9\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5, \u91cd\u7f6e\u7d22\u5f15<\/strong><\/h2>\n<p>filtered_df = filtered_df_loc.reset_index(drop=True)<\/p>\n<h2><strong>\u5c55\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>print(filtered_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u7684\u7b80\u5355DataFrame\uff0c\u7136\u540e\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u5220\u9664\u542b\u6709\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\uff0c\u5e76\u6700\u540e\u91cd\u7f6e\u4e86DataFrame\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5730\u5728\u4e00\u4e2aDataFrame\u4e2d\u7b5b\u9009\u6389\u67d0\u4e00\u5217\u503c\u4e3a0\u7684\u884c\uff0c\u65e0\u8bba\u662f\u5229\u7528<code>loc<\/code>\u8fd8\u662f<code>query<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u52bf\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u4e2a\u4eba\u7f16\u7a0b\u98ce\u683c\u6216\u7279\u5b9a\u573a\u666f\u7684\u9700\u6c42\u9009\u62e9\u4f7f\u7528\u3002\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u6570\u636e\u9884\u5904\u7406\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python\u5220\u9664DataFrame\u4e2d\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\uff1f<\/strong><\/p>\n<p>\u8981\u5220\u9664DataFrame\u4e2d\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u7b5b\u9009\u8981\u5220\u9664\u7684\u884c\u3002\u4e0b\u9762\u662f\u4e00\u79cd\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame\ndf = pd.DataFrame({&#039;A&#039;: [1, 2, 3, 0, 5], &#039;B&#039;: [0, 0, 0, 4, 0]})\n\n# \u5220\u9664&#039;A&#039;\u5217\u5143\u7d20\u4e3a0\u7684\u884c\ndf = df[df[&#039;A&#039;] != 0]\n\nprint(df)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>   A  B\n0  1  0\n1  2  0\n2  3  0\n4  5  0\n<\/code><\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5e03\u5c14\u7d22\u5f15\u6765\u9009\u53d6\u6240\u6709&#039;A&#039;\u5217\u5143\u7d20\u4e0d\u4e3a0\u7684\u884c\uff0c\u7136\u540e\u91cd\u65b0\u5206\u914d\u7ed9\u539f\u59cbDataFrame\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u5220\u9664\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528Python\u5220\u9664DataFrame\u4e2d\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\uff0c\u800c\u4e0d\u4f7f\u7528isin\u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528isin\u51fd\u6570\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u5220\u9664DataFrame\u4e2d\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\u3002\u4ee5\u4e0b\u662f\u53e6\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame\ndf = pd.DataFrame({&#039;A&#039;: [1, 2, 3, 0, 5], &#039;B&#039;: [0, 0, 0, 4, 0]})\n\n# \u5220\u9664&#039;A&#039;\u5217\u5143\u7d20\u4e3a0\u7684\u884c\ndf = df.loc[df[&#039;A&#039;].ne(0)]\n\nprint(df)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u4e0e\u524d\u9762\u7684\u65b9\u6cd5\u76f8\u540c\uff1a<\/p>\n<pre><code>   A  B\n0  1  0\n1  2  0\n2  3  0\n4  5  0\n<\/code><\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86.loc\u548cne\u51fd\u6570\u6765\u9009\u53d6\u6240\u6709&#039;A&#039;\u5217\u5143\u7d20\u4e0d\u7b49\u4e8e0\u7684\u884c\uff0c\u7136\u540e\u91cd\u65b0\u5206\u914d\u7ed9\u539f\u59cbDataFrame\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528Python\u5220\u9664DataFrame\u4e2d\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\uff0c\u907f\u514d\u4f7f\u7528\u9996\u9009\u7684isin\u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u907f\u514d\u4f7f\u7528isin\u51fd\u6570\u7684\u8bdd\uff0c\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u65b9\u6cd5\u6765\u5220\u9664DataFrame\u4e2d\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame\ndf = pd.DataFrame({&#039;A&#039;: [1, 2, 3, 0, 5], &#039;B&#039;: [0, 0, 0, 4, 0]})\n\n# \u5220\u9664&#039;A&#039;\u5217\u5143\u7d20\u4e3a0\u7684\u884c\ndf = df.loc[~(df[&#039;A&#039;] == 0)]\n\nprint(df)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u4e0e\u524d\u9762\u7684\u65b9\u6cd5\u76f8\u540c\uff1a<\/p>\n<pre><code>   A  B\n0  1  0\n1  2  0\n2  3  0\n4  5  0\n<\/code><\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86.loc\u548c~\u8fd0\u7b97\u7b26\u6765\u9009\u53d6\u6240\u6709&#039;A&#039;\u5217\u5143\u7d20\u4e0d\u7b49\u4e8e0\u7684\u884c\uff0c\u7136\u540e\u91cd\u65b0\u5206\u914d\u7ed9\u539f\u59cbDataFrame\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u4f7f\u7528isin\u51fd\u6570\uff0c\u4f46\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5220\u9664DataFrame\u4e2d\u67d0\u5217\u5143\u7d20\u4e3a0\u7684\u884c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528~\u64cd\u4f5c\u7b26\u4ee5\u53caloc\u6216\u8005query\u65b9\u6cd5\u8fdb\u884c\u9009\u62e9\u6027\u8fc7\u6ee4\u64cd\u4f5c\u3002\u6838 [&hellip;]","protected":false},"author":3,"featured_media":175941,"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\/175916"}],"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=175916"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/175916\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/175941"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=175916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=175916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=175916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}