{"id":1008416,"date":"2024-12-27T11:02:32","date_gmt":"2024-12-27T03:02:32","guid":{"rendered":""},"modified":"2024-12-27T11:02:34","modified_gmt":"2024-12-27T03:02:34","slug":"python%e6%a8%aa%e5%90%91%e8%bf%9e%e6%8e%a5%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008416.html","title":{"rendered":"python\u6a2a\u5411\u8fde\u63a5\u5982\u4f55\u5b9e\u73b0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083731\/e3367064-c872-4d52-be26-3153d89ba9cf.webp\" alt=\"python\u6a2a\u5411\u8fde\u63a5\u5982\u4f55\u5b9e\u73b0\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6a2a\u5411\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5982\u5217\u8868\u548c\u5b57\u7b26\u4e32\u7684\u52a0\u6cd5\u8fd0\u7b97\u7b26\u3001NumPy\u5e93\u4e2d\u7684hstack\u51fd\u6570\u3001Pandas\u5e93\u4e2d\u7684concat\u51fd\u6570\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u6570\u636e\u7c7b\u578b\u548c\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0Python\u4e2d\u7684\u6a2a\u5411\u8fde\u63a5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5e94\u7528\u573a\u666f\u7684\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5217\u8868\u7684\u6a2a\u5411\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u7684\u6a2a\u5411\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u52a0\u6cd5\u8fd0\u7b97\u7b26\u6216extend\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\u52a0\u6cd5\u8fd0\u7b97\u7b26<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u52a0\u6cd5\u8fd0\u7b97\u7b26\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>combined_list = list1 + list2<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>extend\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p>extend\u65b9\u6cd5\u7528\u4e8e\u5c06\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>list1.extend(list2)<\/p>\n<p>print(list1)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5b57\u7b26\u4e32\u7684\u6a2a\u5411\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5728Python\u4e2d\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u52a0\u6cd5\u8fd0\u7b97\u7b26\u6216join\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fde\u63a5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u52a0\u6cd5\u8fd0\u7b97\u7b26<\/li>\n<\/ol>\n<p><p>\u52a0\u6cd5\u8fd0\u7b97\u7b26\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello, &quot;<\/p>\n<p>str2 = &quot;World!&quot;<\/p>\n<p>combined_str = str1 + str2<\/p>\n<p>print(combined_str)  # \u8f93\u51fa: Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>join\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p>join\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = [&quot;Hello&quot;, &quot;World&quot;, &quot;Python&quot;]<\/p>\n<p>combined_str = &quot; &quot;.join(str_list)<\/p>\n<p>print(combined_str)  # \u8f93\u51fa: Hello World Python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001NumPy\u5e93\u4e2d\u7684\u6a2a\u5411\u8fde\u63a5<\/p>\n<\/p>\n<p><p>NumPy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u5176\u4e2dhstack\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u6570\u7ec4\u7684\u6a2a\u5411\u8fde\u63a5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528hstack\u51fd\u6570<\/li>\n<\/ol>\n<p><p>hstack\u51fd\u6570\u7528\u4e8e\u6c34\u5e73\u5806\u53e0\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr1 = np.array([1, 2, 3])<\/p>\n<p>arr2 = np.array([4, 5, 6])<\/p>\n<p>combined_arr = np.hstack((arr1, arr2))<\/p>\n<p>print(combined_arr)  # \u8f93\u51fa: [1 2 3 4 5 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528concatenate\u51fd\u6570<\/li>\n<\/ol>\n<p><p>concatenate\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8fde\u63a5\u6570\u7ec4\uff0c\u5e76\u6307\u5b9a\u8fde\u63a5\u7684\u8f74\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr1 = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p>arr2 = np.array([[7, 8, 9], [10, 11, 12]])<\/p>\n<p>combined_arr = np.concatenate((arr1, arr2), axis=1)<\/p>\n<p>print(combined_arr)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>[[ 1  2  3  7  8  9]<\/strong><\/h2>\n<h2><strong> [ 4  5  6 10 11 12]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001Pandas\u5e93\u4e2d\u7684\u6a2a\u5411\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0cPandas\u5e93\u5e38\u7528\u4e8e\u5904\u7406\u6570\u636e\u8868\u683c\uff0cconcat\u548cmerge\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0DataFrame\u7684\u6a2a\u5411\u8fde\u63a5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528concat\u51fd\u6570<\/li>\n<\/ol>\n<p><p>concat\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e24\u4e2a\u6216\u591a\u4e2aDataFrame\u6cbf\u7740\u6307\u5b9a\u8f74\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df1 = pd.DataFrame({&#39;A&#39;: [1, 2], &#39;B&#39;: [3, 4]})<\/p>\n<p>df2 = pd.DataFrame({&#39;C&#39;: [5, 6], &#39;D&#39;: [7, 8]})<\/p>\n<p>combined_df = pd.concat([df1, df2], axis=1)<\/p>\n<p>print(combined_df)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>   A  B  C  D<\/strong><\/h2>\n<h2><strong>0  1  3  5  7<\/strong><\/h2>\n<h2><strong>1  2  4  6  8<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528merge\u51fd\u6570<\/li>\n<\/ol>\n<p><p>merge\u51fd\u6570\u7528\u4e8e\u6839\u636e\u4e00\u4e2a\u6216\u591a\u4e2a\u952e\u5c06\u4e24\u4e2aDataFrame\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df1 = pd.DataFrame({&#39;key&#39;: [&#39;A&#39;, &#39;B&#39;], &#39;value1&#39;: [1, 2]})<\/p>\n<p>df2 = pd.DataFrame({&#39;key&#39;: [&#39;A&#39;, &#39;B&#39;], &#39;value2&#39;: [3, 4]})<\/p>\n<p>combined_df = pd.merge(df1, df2, on=&#39;key&#39;)<\/p>\n<p>print(combined_df)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>  key  value1  value2<\/strong><\/h2>\n<h2><strong>0   A       1       3<\/strong><\/h2>\n<h2><strong>1   B       2       4<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li>\u6570\u636e\u6e05\u6d17\u4e0e\u6574\u5408<\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5e38\u9700\u8981\u5c06\u591a\u4e2a\u6570\u636e\u6e90\u8fdb\u884c\u6574\u5408\uff0c\u4ee5\u4fbf\u8fdb\u884c\u7efc\u5408\u5206\u6790\u3002\u6a2a\u5411\u8fde\u63a5\u53ef\u4ee5\u5e2e\u52a9\u5c06\u4e0d\u540c\u6570\u636e\u6e90\u7684\u6570\u636e\u5408\u5e76\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u7279\u5f81\u5de5\u7a0b<\/li>\n<\/ol>\n<p><p>\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u7279\u5f81\u5de5\u7a0b\u662f\u91cd\u8981\u7684\u4e00\u73af\u3002\u901a\u8fc7\u6a2a\u5411\u8fde\u63a5\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u7279\u5f81\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u7684\u7279\u5f81\u96c6\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u8868\u73b0\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u62a5\u8868\u751f\u6210<\/li>\n<\/ol>\n<p><p>\u5728\u751f\u6210\u62a5\u8868\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u591a\u4e2a\u6570\u636e\u8868\u8fdb\u884c\u5408\u5e76\uff0c\u4ee5\u4fbf\u751f\u6210\u4e00\u4e2a\u7efc\u5408\u7684\u62a5\u544a\u3002\u6a2a\u5411\u8fde\u63a5\u53ef\u4ee5\u5e2e\u52a9\u5c06\u591a\u4e2a\u6570\u636e\u8868\u5408\u5e76\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u62a5\u8868\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u4e2d\u7684\u6a2a\u5411\u8fde\u63a5\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u6570\u636e\u7684\u6a2a\u5411\u8fde\u63a5\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6a2a\u5411\u8fde\u63a5\u4e24\u4e2a\u6570\u636e\u6846\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u4e2d\u7684<code>merge()<\/code>\u6216<code>concat()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u6570\u636e\u6846\u7684\u6a2a\u5411\u8fde\u63a5\u3002<code>merge()<\/code>\u51fd\u6570\u9002\u7528\u4e8e\u57fa\u4e8e\u67d0\u4e9b\u5171\u540c\u5217\u8fdb\u884c\u8fde\u63a5\uff0c\u800c<code>concat()<\/code>\u51fd\u6570\u5219\u66f4\u9002\u5408\u7b80\u5355\u7684\u884c\u6216\u5217\u8fde\u63a5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndf1 = pd.DataFrame({&#39;A&#39;: [1, 2], &#39;B&#39;: [3, 4]})\ndf2 = pd.DataFrame({&#39;C&#39;: [5, 6], &#39;D&#39;: [7, 8]})\n\n# \u4f7f\u7528concat\u8fdb\u884c\u6a2a\u5411\u8fde\u63a5\nresult = pd.concat([df1, df2], axis=1)\nprint(result)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u4e24\u4e2a\u6570\u636e\u6846\u6a2a\u5411\u8fde\u63a5\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846\u3002<\/p>\n<p><strong>\u5728\u6a2a\u5411\u8fde\u63a5\u65f6\uff0c\u5982\u4f55\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u6570\u636e\u6846\uff1f<\/strong><br \/>\u5f53\u5c1d\u8bd5\u6a2a\u5411\u8fde\u63a5\u4e24\u4e2a\u4e0d\u540c\u957f\u5ea6\u7684\u6570\u636e\u6846\u65f6\uff0cPandas\u4f1a\u81ea\u52a8\u586b\u5145\u7f3a\u5931\u503c\u4e3aNaN\u3002\u5982\u679c\u5e0c\u671b\u5728\u8fde\u63a5\u8fc7\u7a0b\u4e2d\u5bf9\u9f50\u6570\u636e\uff0c\u786e\u4fdd\u4e24\u4e2a\u6570\u636e\u6846\u7684\u7d22\u5f15\u662f\u4e00\u81f4\u7684\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>join<\/code>\u53c2\u6570\u7684\u4e0d\u540c\u9009\u9879\u6765\u63a7\u5236\u586b\u5145\u65b9\u5f0f\uff0c\u6bd4\u5982<code>inner<\/code>\u6216<code>outer<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528\u6a2a\u5411\u8fde\u63a5\u65f6\uff0c\u5982\u4f55\u907f\u514d\u5217\u540d\u51b2\u7a81\uff1f<\/strong><br \/>\u5f53\u591a\u4e2a\u6570\u636e\u6846\u5305\u542b\u76f8\u540c\u7684\u5217\u540d\u65f6\uff0c\u6a2a\u5411\u8fde\u63a5\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5217\u540d\u51b2\u7a81\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u8fde\u63a5\u4e4b\u524d\u91cd\u547d\u540d\u5217\uff0c\u6216\u8005\u5728\u4f7f\u7528<code>merge()<\/code>\u65f6\u9009\u62e9\u6dfb\u52a0\u540e\u7f00\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">result = pd.merge(df1, df2, left_on=&#39;A&#39;, right_on=&#39;C&#39;, suffixes=(&#39;_left&#39;, &#39;_right&#39;))\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u6570\u636e\u6846\u7684\u5217\u540d\u5728\u5408\u5e76\u540e\u90fd\u662f\u552f\u4e00\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6a2a\u5411\u8fde\u63a5\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5982\u5217\u8868\u548c\u5b57\u7b26\u4e32\u7684\u52a0\u6cd5\u8fd0\u7b97\u7b26\u3001NumPy\u5e93\u4e2d\u7684hstack [&hellip;]","protected":false},"author":3,"featured_media":1008429,"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\/1008416"}],"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=1008416"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008416\/revisions"}],"predecessor-version":[{"id":1008432,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008416\/revisions\/1008432"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008429"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}