{"id":175304,"date":"2024-05-08T18:48:48","date_gmt":"2024-05-08T10:48:48","guid":{"rendered":""},"modified":"2024-05-08T18:48:54","modified_gmt":"2024-05-08T10:48:54","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%af%b9%e4%b8%89%e4%b8%aa%e3%80%81%e5%9b%9b%e4%b8%aadataframe%e6%b1%82%e4%ba%a4%e9%9b%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/175304.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5bf9\u4e09\u4e2a\u3001\u56db\u4e2adataframe\u6c42\u4ea4\u96c6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27053156\/f0a2b659-2b8b-486f-aec7-d36fe4eed0df.webp\" alt=\"python\u4e2d\u5982\u4f55\u5bf9\u4e09\u4e2a\u3001\u56db\u4e2adataframe\u6c42\u4ea4\u96c6\" \/><\/p>\n<p><p><strong>\u5728Python\u4e2d\u5bf9\u4e09\u4e2a\u3001\u56db\u4e2aDataFrame\u6c42\u4ea4\u96c6\uff0c\u5173\u952e\u5728\u4e8e\u7b5b\u9009\u51fa\u540c\u65f6\u5b58\u5728\u4e8e\u6240\u6709DataFrame\u4e2d\u7684\u5171\u6709\u5143\u7d20\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>merge<\/code>\u51fd\u6570\u5b9e\u73b0\u3001\u901a\u8fc7\u4f7f\u7528<code>concat<\/code>\u51fd\u6570\u7ed3\u5408<code>drop_duplicates<\/code>\u5b9e\u73b0\u3001\u6216\u5229\u9009\u51fa\u5171\u540c\u7684\u7d22\u5f15\u540e\u5229\u7528<code>loc<\/code>\u6216<code>iloc<\/code>\u5b9e\u73b0\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>merge<\/code>\u51fd\u6570\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u8fde\u7eed\u5408\u5e76\u64cd\u4f5c\u5373\u53ef\u5b9e\u73b0\u5bf9\u591a\u4e2aDataFrame\u6c42\u4ea4\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528MERGE\u51fd\u6570\u6c42\u4ea4\u96c6<\/p>\n<\/p>\n<p><p><code>merge<\/code>\u51fd\u6570\u662fPandas\u5e93\u4e2d\u7528\u4e8e\u5408\u5e76DataFrame\u7684\u5de5\u5177\u3002\u8981\u6c42\u591a\u4e2aDataFrame\u7684\u4ea4\u96c6\uff0c\u53ef\u4ee5\u5148\u4e24\u4e24\u5408\u5e76\uff0c\u7136\u540e\u518d\u5408\u5e76\u5176\u4ed6DataFrame\uff0c\u786e\u4fdd\u6700\u540e\u7ed3\u679c\u4e2d\u7684\u5143\u7d20\u662f\u6240\u6709DataFrame\u5171\u6709\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e09\u4e2aDataFrame\uff1adf1, df2, df3<\/strong><\/h2>\n<h2><strong>\u6c42\u4ea4\u96c6\u7684\u64cd\u4f5c\u5982\u4e0b\uff1a<\/strong><\/h2>\n<h2><strong>\u5148\u6c42df1\u548cdf2\u7684\u4ea4\u96c6<\/strong><\/h2>\n<p>df12_intersect = pd.merge(df1, df2, how=&#039;inner&#039;)<\/p>\n<h2><strong>\u518d\u5c06df12_intersect\u548cdf3\u8fdb\u884c\u4ea4\u96c6\u64cd\u4f5c<\/strong><\/h2>\n<p>df123_intersect = pd.merge(df12_intersect, df3, how=&#039;inner&#039;)<\/p>\n<h2><strong>\u5982\u679c\u8fd8\u6709\u7b2c\u56db\u4e2aDataFrame df4:<\/strong><\/h2>\n<p>df1234_intersect = pd.merge(df123_intersect, df4, how=&#039;inner&#039;)<\/p>\n<h2><strong>df1234_intersect\u5c31\u662f\u6700\u7ec8\u7684\u4ea4\u96c6\u7ed3\u679c<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6bcf\u6b21\u64cd\u4f5c\u90fd\u901a\u8fc7<code>how=&#039;inner&#039;<\/code>\u53c2\u6570\u786e\u4fdd\u91c7\u7528\u5185\u8fde\u63a5\uff0c\u4e5f\u5c31\u662f\u4ec5\u4fdd\u7559\u4e24\u4e2aDataFrame\u5171\u6709\u7684\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528CONCAT\u4e0eDROP_DUPLICATES\u51fd\u6570\u7ed3\u5408\u6c42\u4ea4\u96c6<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u6240\u6709DataFrame\u5408\u5e76\u5230\u4e00\u8d77\uff0c\u7136\u540e\u5220\u9664\u91cd\u590d\u7684\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e09\u4e2aDataFrame\uff1adf1, df2, df3<\/strong><\/h2>\n<h2><strong>\u5c06\u4e09\u4e2aDataFrame\u5408\u5e76\u5230\u4e00\u8d77<\/strong><\/h2>\n<p>df_concatenated = pd.concat([df1, df2, df3])<\/p>\n<h2><strong>\u901a\u8fc7drop_duplicates\u5220\u9664\u91cd\u590d\u6570\u636e\uff0c\u4fdd\u7559\u552f\u4e00\u7684\u4ea4\u96c6\u90e8\u5206<\/strong><\/h2>\n<p>df_intersect = df_concatenated.drop_duplicates(keep=False)<\/p>\n<h2><strong>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5bf9DataFrame\u6c42\u4ea4\u96c6\u65f6\u53ef\u80fd\u9700\u8981\u4fdd\u8bc1\u5217\u540d\u4e00\u81f4\u4e14\u65e0\u91cd\u590d\u884c<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>pd.concat()<\/code>\u5806\u53e0DataFrame\uff0c\u518d\u4f7f\u7528<code>drop_duplicates(keep=False)<\/code>\u5220\u9664\u6240\u6709\u91cd\u590d\u7684\u884c\uff0c\u552f\u4e00\u5269\u4e0b\u7684\u5c31\u662f\u4ea4\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7d22\u5f15\u8fdb\u884c\u4ea4\u96c6\u7b5b\u9009<\/p>\n<\/p>\n<p><p>\u5982\u679cDataFrame\u5171\u6709\u7684\u90e8\u5206\u4e3b\u8981\u4f53\u73b0\u5728\u7d22\u5f15\u4e0a\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7\u83b7\u53d6\u5171\u540c\u7684\u7d22\u5f15\u540e\uff0c\u518d\u7ecf\u8fc7\u7d22\u5f15\u7b5b\u9009\u6765\u83b7\u53d6\u4ea4\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5171\u6709\u7d22\u5f15<\/p>\n<p>common_index = df1.index.intersection(df2.index).intersection(df3.index)<\/p>\n<h2><strong>\u5bf9\u7b2c\u4e00\u4e2aDataFrame\u8fdb\u884c\u7b5b\u9009<\/strong><\/h2>\n<p>df1_common = df1.loc[common_index]<\/p>\n<h2><strong>\u5bf9\u540e\u7eedDataFrame\u8fdb\u884c\u540c\u6837\u7684\u7b5b\u9009<\/strong><\/h2>\n<p>df2_common = df2.loc[common_index]<\/p>\n<p>df3_common = df3.loc[common_index]<\/p>\n<h2><strong>\u901a\u8fc7\u7d22\u5f15\u786e\u4fdd\u7b5b\u9009\u51fa\u7684\u662f\u4ea4\u96c6\u90e8\u5206<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u53ef\u5e94\u7528\u4e8e\u4e09\u4e2a\u3001\u56db\u4e2a\u6216\u66f4\u591aDataFrame\u7684\u4ea4\u96c6\u6c42\u89e3\u3002\u91cd\u8981\u7684\u662f\u6839\u636e\u5b9e\u9645\u7684\u6570\u636e\u7ed3\u6784\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u786e\u4fdd\u6700\u7ec8\u7684\u7ed3\u679c\u51c6\u786e\u65e0\u8bef\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728Python\u4e2d\u5bf9\u4e09\u4e2adataframe\u6c42\u4ea4\u96c6\uff1f<\/strong><\/p>\n<p>\u8981\u5bf9\u4e09\u4e2adataframe\u6c42\u4ea4\u96c6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u4e2d\u7684<code>merge<\/code>\u51fd\u6570\u3002\u5047\u8bbe\u4f60\u6709\u4e09\u4e2adataframe\uff0c\u5206\u522b\u547d\u540d\u4e3adf1\u3001df2\u548cdf3\u3002\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>merge<\/code>\u51fd\u6570\u5c06df1\u548cdf2\u7684\u4ea4\u96c6\u4fdd\u5b58\u4e3a\u4e00\u4e2a\u65b0\u7684dataframe\uff1a<\/p>\n<pre><code>df_intersection = df1.merge(df2, how=&#039;inner&#039;)\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528<code>merge<\/code>\u51fd\u6570\u5c06\u65b0\u7684dataframe\u4e0edf3\u6c42\u4ea4\u96c6\uff1a<\/p>\n<pre><code>df_intersection = df_intersection.merge(df3, how=&#039;inner&#039;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0cdf_intersection\u5c06\u4fdd\u5b58df1\u3001df2\u548cdf3\u7684\u4ea4\u96c6\u3002<\/p>\n<p><strong>2. \u5728Python\u4e2d\u5982\u4f55\u5bf9\u56db\u4e2adataframe\u6c42\u4ea4\u96c6\uff1f<\/strong><\/p>\n<p>\u5bf9\u4e8e\u56db\u4e2adataframe\u7684\u4ea4\u96c6\uff0c\u4f60\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528<code>merge<\/code>\u51fd\u6570\u3002\u5047\u8bbe\u4f60\u6709\u56db\u4e2adataframe\uff0c\u5206\u522b\u547d\u540d\u4e3adf1\u3001df2\u3001df3\u548cdf4\u3002\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>merge<\/code>\u51fd\u6570\u5c06df1\u548cdf2\u7684\u4ea4\u96c6\u4fdd\u5b58\u4e3a\u4e00\u4e2a\u65b0\u7684dataframe\uff1a<\/p>\n<pre><code>df_intersection = df1.merge(df2, how=&#039;inner&#039;)\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528<code>merge<\/code>\u51fd\u6570\u5c06\u65b0\u7684dataframe\u4e0edf3\u6c42\u4ea4\u96c6\uff1a<\/p>\n<pre><code>df_intersection = df_intersection.merge(df3, how=&#039;inner&#039;)\n<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>merge<\/code>\u51fd\u6570\u5c06\u65b0\u7684dataframe\u4e0edf4\u6c42\u4ea4\u96c6\uff1a<\/p>\n<pre><code>df_intersection = df_intersection.merge(df4, how=&#039;inner&#039;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0cdf_intersection\u5c06\u4fdd\u5b58df1\u3001df2\u3001df3\u548cdf4\u7684\u4ea4\u96c6\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728Python\u4e2d\u5bf9\u4efb\u610f\u6570\u91cf\u7684dataframe\u6c42\u4ea4\u96c6\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u6709\u4efb\u610f\u6570\u91cf\u7684dataframe\uff0c\u5e76\u4e14\u60f3\u8981\u6c42\u5b83\u4eec\u7684\u4ea4\u96c6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c<code>merge<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u5c06\u6240\u6709\u7684dataframe\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u7136\u540e\uff0c\u4f7f\u7528\u7b2c\u4e00\u4e2adataframe\u4f5c\u4e3a\u521d\u59cb\u4ea4\u96c6\uff0c\u9010\u4e2a\u5c06\u5176\u4ed6dataframe\u4e0e\u4ea4\u96c6\u6c42\u4ea4\u96c6\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">df_list = [df1, df2, df3, df4, ...]\n\ndf_intersection = df_list[0] # \u4f7f\u7528\u7b2c\u4e00\u4e2adataframe\u4f5c\u4e3a\u521d\u59cb\u4ea4\u96c6\n\nfor df in df_list[1:]:\n    df_intersection = df_intersection.merge(df, how=&#039;inner&#039;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0cdf_intersection\u5c06\u4fdd\u5b58\u6240\u6709dataframe\u7684\u4ea4\u96c6\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574df_list\u4e2d\u7684dataframe\u6570\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5bf9\u4e09\u4e2a\u3001\u56db\u4e2aDataFrame\u6c42\u4ea4\u96c6\uff0c\u5173\u952e\u5728\u4e8e\u7b5b\u9009\u51fa\u540c\u65f6\u5b58\u5728\u4e8e\u6240\u6709DataFrame\u4e2d\u7684\u5171\u6709\u5143 [&hellip;]","protected":false},"author":3,"featured_media":175314,"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\/175304"}],"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=175304"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/175304\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/175314"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=175304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=175304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=175304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}