{"id":1108151,"date":"2025-01-08T16:57:21","date_gmt":"2025-01-08T08:57:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108151.html"},"modified":"2025-01-08T16:57:24","modified_gmt":"2025-01-08T08:57:24","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%97%e5%90%8d%e6%b7%bb%e5%8a%a0%e5%88%b0%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108151.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5217\u540d\u6dfb\u52a0\u5230\u7d22\u5f15"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071909\/5aa72d1b-7263-4516-91dd-e07d46e839b5.webp\" alt=\"python\u5982\u4f55\u5c06\u5217\u540d\u6dfb\u52a0\u5230\u7d22\u5f15\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u5217\u540d\u6dfb\u52a0\u5230\u7d22\u5f15\u4e2d\uff0c\u5305\u62ec\u4f7f\u7528pandas\u5e93\u7684set_index\u65b9\u6cd5\u3001\u91cd\u547d\u540d\u7d22\u5f15\u7b49\u3002<\/strong>\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528pandas\u4e2d\u7684set_index\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u5f3a\u5927\u5e93\u3002\u8981\u5c06\u5217\u540d\u6dfb\u52a0\u5230\u7d22\u5f15\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u7684set_index\u65b9\u6cd5\u3002\u8fd9\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\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 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528set_index\u65b9\u6cd5\u5c06\u5217B\u8bbe\u7f6e\u4e3a\u7d22\u5f15<\/strong><\/h2>\n<p>df.set_index(&#39;B&#39;, inplace=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aDataFrame\uff0c\u5e76\u4f7f\u7528set_index\u65b9\u6cd5\u5c06\u5217B\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\u4e14\u5bb9\u6613\u7406\u89e3<\/strong>\uff0c\u7279\u522b\u9002\u5408\u7b80\u5355\u7684\u6570\u636e\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528pandas\u4e2d\u7684reset_index\u65b9\u6cd5\u7136\u540e\u91cd\u547d\u540d\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u5148\u5c06\u7d22\u5f15\u91cd\u7f6e\uff0c\u7136\u540e\u518d\u5c06\u65b0\u7684\u5217\u540d\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002\u53ef\u4ee5\u4f7f\u7528reset_index\u65b9\u6cd5\uff0c\u7136\u540e\u91cd\u547d\u540d\u7d22\u5f15\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 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u91cd\u7f6e\u7d22\u5f15\u5e76\u5c06\u5217B\u8bbe\u7f6e\u4e3a\u7d22\u5f15<\/strong><\/h2>\n<p>df = df.reset_index().set_index(&#39;B&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528reset_index\u65b9\u6cd5\u91cd\u7f6e\u7d22\u5f15\uff0c\u7136\u540e\u518d\u4f7f\u7528set_index\u65b9\u6cd5\u5c06\u5217B\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u7d22\u5f15\u8fdb\u884c\u591a\u6b21\u64cd\u4f5c\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528pandas\u4e2d\u7684rename_axis\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u76f4\u63a5\u91cd\u547d\u540d\u7d22\u5f15\u8f74\uff0c\u800c\u4e0d\u662f\u5c06\u5217\u540d\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002\u53ef\u4ee5\u4f7f\u7528rename_axis\u65b9\u6cd5\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 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06\u5217B\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u5e76\u91cd\u547d\u540d\u7d22\u5f15\u8f74<\/strong><\/h2>\n<p>df.set_index(&#39;B&#39;, inplace=True)<\/p>\n<p>df = df.rename_axis(&#39;New_Index&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528rename_axis\u65b9\u6cd5\u5c06\u7d22\u5f15\u8f74\u91cd\u547d\u540d\u4e3a&quot;New_Index&quot;\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u7d22\u5f15\u8f74\u8fdb\u884c\u91cd\u547d\u540d\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528pandas\u4e2d\u7684multi-index\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u591a\u4e2a\u5217\u540d\u8bbe\u7f6e\u4e3a\u591a\u7ea7\u7d22\u5f15\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u7684MultiIndex.from_frame\u65b9\u6cd5\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 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528MultiIndex.from_frame\u65b9\u6cd5\u5c06\u591a\u4e2a\u5217\u540d\u8bbe\u7f6e\u4e3a\u591a\u7ea7\u7d22\u5f15<\/strong><\/h2>\n<p>df.set_index([&#39;A&#39;, &#39;B&#39;], inplace=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528MultiIndex.from_frame\u65b9\u6cd5\u5c06\u5217A\u548c\u5217B\u8bbe\u7f6e\u4e3a\u591a\u7ea7\u7d22\u5f15\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u591a\u7ea7\u7d22\u5f15\u7684\u6570\u636e\u7ed3\u6784<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528pandas\u4e2d\u7684assign\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u5728\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\u4e4b\u524d\u5148\u8fdb\u884c\u4e00\u4e9b\u6570\u636e\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u7684assign\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\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 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528assign\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u5904\u7406\u5e76\u5c06\u5217B\u8bbe\u7f6e\u4e3a\u7d22\u5f15<\/strong><\/h2>\n<p>df = df.assign(D=lambda x: x[&#39;A&#39;] + x[&#39;B&#39;]).set_index(&#39;D&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528assign\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5217D\uff0c\u7136\u540e\u5c06\u5217D\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\u4e4b\u524d\u8fdb\u884c\u6570\u636e\u5904\u7406\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528pandas\u4e2d\u7684loc\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528loc\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\u3002loc\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u6309\u6807\u7b7e\u8fdb\u884c\u9009\u62e9\u548c\u64cd\u4f5c\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 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528loc\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/strong><\/h2>\n<p>df = df.set_index(&#39;B&#39;).loc[:, [&#39;A&#39;, &#39;C&#39;]]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528loc\u65b9\u6cd5\u9009\u62e9\u4e86\u5217A\u548c\u5217C\uff0c\u7136\u540e\u5c06\u5217B\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6309\u6807\u7b7e\u8fdb\u884c\u9009\u62e9\u548c\u64cd\u4f5c\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528pandas\u4e2d\u7684apply\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528apply\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\u3002apply\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5bf9DataFrame\u7684\u884c\u6216\u5217\u8fdb\u884c\u5e94\u7528\u51fd\u6570\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 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528apply\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/strong><\/h2>\n<p>df[&#39;D&#39;] = df.apply(lambda row: row[&#39;A&#39;] + row[&#39;B&#39;], axis=1)<\/p>\n<p>df.set_index(&#39;D&#39;, inplace=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528apply\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5217D\uff0c\u7136\u540e\u5c06\u5217D\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9DataFrame\u7684\u884c\u6216\u5217\u8fdb\u884c\u5e94\u7528\u51fd\u6570\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528pandas\u4e2d\u7684groupby\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528groupby\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\u3002groupby\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5bf9DataFrame\u8fdb\u884c\u5206\u7ec4\u64cd\u4f5c\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 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528groupby\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/strong><\/h2>\n<p>df = df.groupby(&#39;B&#39;).sum()<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528groupby\u65b9\u6cd5\u5bf9\u5217B\u8fdb\u884c\u4e86\u5206\u7ec4\u64cd\u4f5c\uff0c\u5e76\u8ba1\u7b97\u4e86\u6bcf\u7ec4\u7684\u603b\u548c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9DataFrame\u8fdb\u884c\u5206\u7ec4\u64cd\u4f5c\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u4f7f\u7528pandas\u4e2d\u7684pivot_table\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528pivot_table\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\u3002pivot_table\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u900f\u89c6\u8868\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 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528pivot_table\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/strong><\/h2>\n<p>df = df.pivot_table(index=&#39;B&#39;, values=&#39;A&#39;, aggfunc=&#39;sum&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pivot_table\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u900f\u89c6\u8868\uff0c\u5e76\u5c06\u5217B\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u521b\u5efa\u900f\u89c6\u8868\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u4f7f\u7528pandas\u4e2d\u7684merge\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528merge\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\u3002merge\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5c06\u4e24\u4e2aDataFrame\u5408\u5e76\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data1 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>data2 = {&#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df1 = pd.DataFrame(data1)<\/p>\n<p>df2 = pd.DataFrame(data2)<\/p>\n<h2><strong>\u4f7f\u7528merge\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/strong><\/h2>\n<p>df = pd.merge(df1, df2, on=&#39;B&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528merge\u65b9\u6cd5\u5c06\u4e24\u4e2aDataFrame\u5408\u5e76\u5728\u4e00\u8d77\uff0c\u5e76\u5c06\u5217B\u4f5c\u4e3a\u7d22\u5f15\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5408\u5e76\u591a\u4e2aDataFrame\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u5341\u4e00\u3001\u4f7f\u7528pandas\u4e2d\u7684concat\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528concat\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\u3002concat\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5c06\u591a\u4e2aDataFrame\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data1 = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>data2 = {&#39;A&#39;: [7, 8, 9], &#39;B&#39;: [10, 11, 12]}<\/p>\n<p>df1 = pd.DataFrame(data1)<\/p>\n<p>df2 = pd.DataFrame(data2)<\/p>\n<h2><strong>\u4f7f\u7528concat\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/strong><\/h2>\n<p>df = pd.concat([df1, df2], ignore_index=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528concat\u65b9\u6cd5\u5c06\u4e24\u4e2aDataFrame\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u5e76\u91cd\u7f6e\u4e86\u7d22\u5f15\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fde\u63a5\u591a\u4e2aDataFrame\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u5341\u4e8c\u3001\u4f7f\u7528pandas\u4e2d\u7684join\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528join\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\u3002join\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5c06\u4e24\u4e2aDataFrame\u6309\u7d22\u5f15\u8fdb\u884c\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data1 = {&#39;A&#39;: [1, 2, 3]}<\/p>\n<p>data2 = {&#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df1 = pd.DataFrame(data1)<\/p>\n<p>df2 = pd.DataFrame(data2)<\/p>\n<h2><strong>\u4f7f\u7528join\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/strong><\/h2>\n<p>df = df1.join(df2)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528join\u65b9\u6cd5\u5c06\u4e24\u4e2aDataFrame\u6309\u7d22\u5f15\u8fdb\u884c\u4e86\u8fde\u63a5\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6309\u7d22\u5f15\u8fde\u63a5\u591a\u4e2aDataFrame\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5341\u4e8c\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5c06\u5217\u540d\u6dfb\u52a0\u5230\u7d22\u5f15\u4e2d\uff0c\u5904\u7406\u5404\u79cd\u6570\u636e\u64cd\u4f5c\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u7559\u8a00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5217\u540d\u4f5c\u4e3a\u7d22\u5f15\u6dfb\u52a0\u5230DataFrame\u4e2d\uff1f<\/strong><br \/>\u8981\u5c06\u5217\u540d\u6dfb\u52a0\u4e3a\u7d22\u5f15\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>set_index()<\/code>\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u5e76\u5bfc\u5165Pandas\u5e93\uff0c\u7136\u540e\u901a\u8fc7\u6307\u5b9a\u5217\u540d\u5c06\u5176\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002\u4f8b\u5982\uff1a<code>df.set_index(&#39;\u5217\u540d&#39;, inplace=True)<\/code>\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u5217\u540d\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u5217\u540d\u4f5c\u4e3a\u7d22\u5f15\u65f6\uff0c\u6709\u4ec0\u4e48\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u5c06\u5217\u540d\u6dfb\u52a0\u4e3a\u7d22\u5f15\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u8be5\u5217\u4e2d\u7684\u503c\u662f\u552f\u4e00\u7684\uff0c\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931\u6216\u7d22\u5f15\u91cd\u590d\u3002\u5982\u679c\u5217\u4e2d\u5b58\u5728\u91cd\u590d\u503c\uff0c\u5efa\u8bae\u5728\u8bbe\u7f6e\u7d22\u5f15\u4e4b\u524d\u5904\u7406\u8fd9\u4e9b\u91cd\u590d\u9879\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5c06\u591a\u4e2a\u5217\u540d\u540c\u65f6\u6dfb\u52a0\u4e3a\u7d22\u5f15\u5417\uff1f<\/strong><br \/>\u53ef\u4ee5\uff0cPandas\u5141\u8bb8\u4f7f\u7528\u591a\u4e2a\u5217\u540d\u8bbe\u7f6e\u590d\u5408\u7d22\u5f15\u3002\u901a\u8fc7\u5c06\u5217\u540d\u4f20\u9012\u4e3a\u5217\u8868\uff0c\u4f8b\u5982\uff1a<code>df.set_index([&#39;\u5217\u540d1&#39;, &#39;\u5217\u540d2&#39;], inplace=True)<\/code>\uff0c\u60a8\u5c31\u53ef\u4ee5\u5c06\u591a\u4e2a\u5217\u540c\u65f6\u4f5c\u4e3a\u7d22\u5f15\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u591a\u5c42\u6b21\u6570\u636e\u65f6\u5c24\u4e3a\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u5217\u540d\u6dfb\u52a0\u5230\u7d22\u5f15\u4e2d\uff0c\u5305\u62ec\u4f7f\u7528pandas\u5e93\u7684set_index\u65b9\u6cd5\u3001\u91cd\u547d\u540d\u7d22\u5f15\u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1108166,"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\/1108151"}],"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=1108151"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108151\/revisions"}],"predecessor-version":[{"id":1108169,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108151\/revisions\/1108169"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108166"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}