{"id":1001306,"date":"2024-12-27T09:58:54","date_gmt":"2024-12-27T01:58:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001306.html"},"modified":"2024-12-27T09:58:57","modified_gmt":"2024-12-27T01:58:57","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2%e6%95%b0%e6%8d%ae%e5%bd%a2%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001306.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u6362\u6570\u636e\u5f62\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075523\/23672745-8548-46d3-9ec6-be2fe2868442.webp\" alt=\"python\u5982\u4f55\u8f6c\u6362\u6570\u636e\u5f62\u5f0f\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8f6c\u6362\u6570\u636e\u5f62\u5f0f\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8f6c\u6362\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u4f7f\u7528pandas\u5e93\u8f6c\u6362\u6570\u636e\u6846\u683c\u5f0f\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u6570\u7ec4\u8f6c\u6362\u3002\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u6d6e\u70b9\u6570\u6216\u5e03\u5c14\u503c\u3002\u4f7f\u7528pandas\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7DataFrame\u7684\u5404\u79cd\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff0c\u6bd4\u5982astype()\u65b9\u6cd5\u3002NumPy\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u7ec4\u8f6c\u6362\u65b9\u6cd5\uff0c\u5982reshape()\u3001transpose()\u7b49\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u4e2a\u65b9\u9762\uff1a\u4f7f\u7528pandas\u5e93\u8f6c\u6362\u6570\u636e\u6846\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u6570\u636e\u6846\uff08DataFrame\uff09\u662fpandas\u4e2d\u6700\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u5e38\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u4e8c\u7ef4\u6570\u636e\u3002\u5728\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u4f7f\u7528pandas\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u6846\u4e2d\u7684\u5217\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5217\uff0c\u5982\u679c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528DataFrame\u7684astype()\u65b9\u6cd5\u3002\u901a\u8fc7\u6307\u5b9a\u76ee\u6807\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u5c06\u5217\u4e2d\u7684\u6240\u6709\u503c\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u3002\u8fd9\u79cd\u8f6c\u6362\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u5982\u4ece\u5b57\u7b26\u4e32\u5230\u6d6e\u70b9\u6570\u3001\u4ece\u6d6e\u70b9\u6570\u5230\u6574\u6570\u7b49\u3002\u6b64\u5916\uff0cpandas\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u65b9\u6cd5\uff0c\u5982to_numeric()\u3001to_datetime()\u7b49\uff0c\u7528\u4e8e\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u503c\u6216\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u95ee\u9898\uff0c\u5982\u6570\u636e\u683c\u5f0f\u4e0d\u4e00\u81f4\u3001\u7f3a\u5931\u503c\u7b49\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e9b\u95ee\u9898\uff0cpandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u53c2\u6570\uff0c\u4ee5\u5e2e\u52a9\u7528\u6237\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570errors=&#39;coerce&#39;\u6765\u5904\u7406\u65e0\u6cd5\u8f6c\u6362\u7684\u503c\uff0c\u5c06\u5176\u66ff\u6362\u4e3aNaN\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\uff0c\u7528\u6237\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Python\u4e2d\u5176\u4ed6\u5e38\u7528\u7684\u6570\u636e\u8f6c\u6362\u65b9\u6cd5\uff0c\u63d0\u4f9b\u8be6\u7ec6\u7684\u793a\u4f8b\u548c\u5b9e\u8df5\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8f6c\u6362\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u8fd9\u4e9b\u51fd\u6570\u5305\u62ecint()\u3001float()\u3001str()\u3001bool()\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u548c\u5e03\u5c14\u503c\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u5b58\u50a8\u3002\u56e0\u6b64\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u662f\u5e38\u89c1\u7684\u9700\u6c42\u3002Python\u7684int()\u548cfloat()\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>num_str = &quot;123&quot;<\/p>\n<p>num_int = int(num_str)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>num_str_float = &quot;123.45&quot;<\/p>\n<p>num_float = float(num_str_float)<\/p>\n<p>print(num_float)  # \u8f93\u51fa: 123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u8f6c\u6362\u5c06\u4f1a\u5931\u8d25\u5e76\u629b\u51fa\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u5728\u8f6c\u6362\u4e4b\u524d\uff0c\u786e\u4fdd\u5b57\u7b26\u4e32\u4ec5\u5305\u542b\u6570\u5b57\u5b57\u7b26\u662f\u5f88\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u5904\u7406\u6216\u8f93\u51fa\u3002Python\u7684str()\u51fd\u6570\u53ef\u4ee5\u5c06\u6574\u6570\u6216\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>num = 456<\/p>\n<p>num_str = str(num)<\/p>\n<p>print(num_str)  # \u8f93\u51fa: &quot;456&quot;<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>num_float = 789.01<\/p>\n<p>num_str_float = str(num_float)<\/p>\n<p>print(num_str_float)  # \u8f93\u51fa: &quot;789.01&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u5e03\u5c14\u503c\u4e0e\u5176\u4ed6\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362<\/li>\n<\/ol>\n<p><p>\u5e03\u5c14\u503cTrue\u548cFalse\u5728Python\u4e2d\u53ef\u4ee5\u8f6c\u6362\u4e3a\u6574\u65701\u548c0\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u4f7f\u7528bool()\u51fd\u6570\u53ef\u4ee5\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff0c\u5176\u4e2d0\u3001\u7a7a\u5b57\u7b26\u4e32\u3001None\u7b49\u4f1a\u88ab\u8f6c\u6362\u4e3aFalse\uff0c\u5176\u4ed6\u503c\u5219\u4e3aTrue\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5e03\u5c14\u503c\u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>bool_value = True<\/p>\n<p>int_value = int(bool_value)<\/p>\n<p>print(int_value)  # \u8f93\u51fa: 1<\/p>\n<h2><strong>\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c<\/strong><\/h2>\n<p>num = 0<\/p>\n<p>bool_value = bool(num)<\/p>\n<p>print(bool_value)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528pandas\u5e93\u8f6c\u6362\u6570\u636e\u6846\u683c\u5f0f<\/p>\n<\/p>\n<p><p>Pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u6570\u636e\u6846\uff08DataFrame\uff09\u662fpandas\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u7c7b\u4f3c\u4e8e\u7535\u5b50\u8868\u683c\u6216SQL\u8868\u683c\u3002\u5728\u6570\u636e\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u5217\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528astype()\u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362<\/li>\n<\/ol>\n<p><p>pandas\u7684DataFrame\u5bf9\u8c61\u63d0\u4f9b\u4e86astype()\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06\u5217\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\u3002\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u6216\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;], &#39;B&#39;: [&#39;4.5&#39;, &#39;5.6&#39;, &#39;6.7&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06\u5217A\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b<\/strong><\/h2>\n<p>df[&#39;A&#39;] = df[&#39;A&#39;].astype(int)<\/p>\n<h2><strong>\u5c06\u5217B\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b<\/strong><\/h2>\n<p>df[&#39;B&#39;] = df[&#39;B&#39;].astype(float)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u6570\u636e\u683c\u5f0f\u4e0d\u7b26\u5408\u76ee\u6807\u7c7b\u578b\uff0castype()\u65b9\u6cd5\u4f1a\u629b\u51fa\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570errors=&#39;coerce&#39;\u6765\u5904\u7406\u65e0\u6cd5\u8f6c\u6362\u7684\u503c\uff0c\u5c06\u5176\u66ff\u6362\u4e3aNaN\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528to_numeric()\u65b9\u6cd5\u8f6c\u6362\u6570\u503c<\/li>\n<\/ol>\n<p><p>pandas\u63d0\u4f9b\u4e86to_numeric()\u65b9\u6cd5\uff0c\u4e13\u95e8\u7528\u4e8e\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b\u3002\u5b83\u53ef\u4ee5\u5904\u7406\u6df7\u5408\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5e76\u5c06\u65e0\u6cd5\u8f6c\u6362\u7684\u503c\u66ff\u6362\u4e3aNaN\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6df7\u5408\u7c7b\u578b\u6570\u636e\u7684\u6570\u636e\u6846<\/p>\n<p>data = {&#39;A&#39;: [&#39;1&#39;, &#39;2&#39;, &#39;three&#39;], &#39;B&#39;: [&#39;4.5&#39;, &#39;five&#39;, &#39;6.7&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06\u5217A\u548cB\u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b<\/strong><\/h2>\n<p>df[&#39;A&#39;] = pd.to_numeric(df[&#39;A&#39;], errors=&#39;coerce&#39;)<\/p>\n<p>df[&#39;B&#39;] = pd.to_numeric(df[&#39;B&#39;], errors=&#39;coerce&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528to_datetime()\u65b9\u6cd5\u8f6c\u6362\u65e5\u671f\u65f6\u95f4<\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u65e5\u671f\u65f6\u95f4\u6570\u636e\u7684\u5904\u7406\u4e5f\u975e\u5e38\u91cd\u8981\u3002pandas\u63d0\u4f9b\u4e86to_datetime()\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65e5\u671f\u65f6\u95f4\u6570\u636e\u8f6c\u6362\u4e3apandas\u7684datetime\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u6570\u636e\u6846<\/p>\n<p>data = {&#39;Date&#39;: [&#39;2023-01-01&#39;, &#39;2023\/02\/01&#39;, &#39;01-03-2023&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06\u5217Date\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u7c7b\u578b<\/strong><\/h2>\n<p>df[&#39;Date&#39;] = pd.to_datetime(df[&#39;Date&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>to_datetime()\u65b9\u6cd5\u652f\u6301\u591a\u79cd\u65e5\u671f\u683c\u5f0f\uff0c\u5e76\u80fd\u591f\u81ea\u52a8\u89e3\u6790\u5927\u591a\u6570\u5e38\u89c1\u683c\u5f0f\u3002\u5982\u679c\u65e5\u671f\u683c\u5f0f\u590d\u6742\u6216\u4e0d\u5e38\u89c1\uff0c\u53ef\u4ee5\u901a\u8fc7format\u53c2\u6570\u6307\u5b9a\u5177\u4f53\u7684\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u6570\u7ec4\u8f6c\u6362<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u91cd\u8981\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5904\u7406\u529f\u80fd\u3002\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0cNumPy\u6570\u7ec4\u5e38\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u5927\u89c4\u6a21\u6570\u636e\u3002\u5728\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u7ec4\u8fdb\u884c\u5f62\u72b6\u3001\u7c7b\u578b\u7b49\u8f6c\u6362\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528reshape()\u65b9\u6cd5\u6539\u53d8\u6570\u7ec4\u5f62\u72b6<\/li>\n<\/ol>\n<p><p>NumPy\u7684reshape()\u65b9\u6cd5\u7528\u4e8e\u6539\u53d8\u6570\u7ec4\u7684\u5f62\u72b6\uff0c\u800c\u4e0d\u6539\u53d8\u5176\u6570\u636e\u3002\u53ef\u4ee5\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u591a\u7ef4\u6570\u7ec4\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a2x3\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr_reshaped = arr.reshape(2, 3)<\/p>\n<p>print(arr_reshaped)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0creshape()\u65b9\u6cd5\u8981\u6c42\u65b0\u5f62\u72b6\u7684\u603b\u5143\u7d20\u6570\u91cf\u4e0e\u539f\u6570\u7ec4\u76f8\u540c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528transpose()\u65b9\u6cd5\u8fdb\u884c\u6570\u7ec4\u8f6c\u7f6e<\/li>\n<\/ol>\n<p><p>\u8f6c\u7f6e\u662f\u6307\u5c06\u6570\u7ec4\u7684\u884c\u548c\u5217\u4e92\u6362\u3002NumPy\u7684transpose()\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5bf9\u591a\u7ef4\u6570\u7ec4\u8fdb\u884c\u8f6c\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a2x3\u7684\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>arr = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u5bf9\u6570\u7ec4\u8fdb\u884c\u8f6c\u7f6e<\/strong><\/h2>\n<p>arr_transposed = arr.transpose()<\/p>\n<p>print(arr_transposed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528astype()\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/li>\n<\/ol>\n<p><p>NumPy\u6570\u7ec4\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5e03\u5c14\u503c\u7b49\u3002\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u7ec4\u7684\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\u3002NumPy\u7684astype()\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u6574\u6570\u6570\u7ec4<\/p>\n<p>arr = np.array([1, 2, 3])<\/p>\n<h2><strong>\u5c06\u6574\u6570\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u6570\u7ec4<\/strong><\/h2>\n<p>arr_float = arr.astype(float)<\/p>\n<p>print(arr_float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6570\u636e\u8f6c\u6362\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u5982\u6570\u636e\u683c\u5f0f\u4e0d\u4e00\u81f4\u3001\u7f3a\u5931\u503c\u3001\u5f02\u5e38\u503c\u7b49\u3002\u9488\u5bf9\u8fd9\u4e9b\u95ee\u9898\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u4ee5\u5e2e\u52a9\u7528\u6237\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5904\u7406\u7f3a\u5931\u503c<\/li>\n<\/ol>\n<p><p>\u7f3a\u5931\u503c\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u7684\u5206\u6790\u7ed3\u679c\u3002\u5728\u6570\u636e\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u7684fillna()\u65b9\u6cd5\u586b\u5145\u7f3a\u5931\u503c\uff0c\u6216\u4f7f\u7528dropna()\u65b9\u6cd5\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u6216\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7f3a\u5931\u503c\u7684\u6570\u636e\u6846<\/p>\n<p>data = {&#39;A&#39;: [1, 2, None], &#39;B&#39;: [4, None, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df_filled = df.fillna(0)<\/p>\n<p>print(df_filled)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5904\u7406\u5f02\u5e38\u503c<\/li>\n<\/ol>\n<p><p>\u5f02\u5e38\u503c\u662f\u6307\u4e0e\u5176\u4ed6\u6570\u636e\u663e\u8457\u4e0d\u540c\u7684\u503c\uff0c\u53ef\u80fd\u662f\u6570\u636e\u5f55\u5165\u9519\u8bef\u6216\u5f02\u5e38\u4e8b\u4ef6\u5f15\u8d77\u7684\u3002\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5f02\u5e38\u503c\u53ef\u80fd\u4f1a\u5f71\u54cd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002\u53ef\u4ee5\u4f7f\u7528\u7edf\u8ba1\u65b9\u6cd5\u6216\u53ef\u89c6\u5316\u5de5\u5177\u8bc6\u522b\u5f02\u5e38\u503c\uff0c\u5e76\u6839\u636e\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5904\u7406\u6570\u636e\u683c\u5f0f\u4e0d\u4e00\u81f4<\/li>\n<\/ol>\n<p><p>\u6570\u636e\u683c\u5f0f\u4e0d\u4e00\u81f4\u53ef\u80fd\u5bfc\u81f4\u8f6c\u6362\u5931\u8d25\u3002\u5728\u8f6c\u6362\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u4e00\u81f4\u3002\u5bf9\u4e8e\u65e5\u671f\u65f6\u95f4\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u7684to_datetime()\u65b9\u6cd5\u81ea\u52a8\u89e3\u6790\u591a\u79cd\u65e5\u671f\u683c\u5f0f\uff0c\u6216\u901a\u8fc7format\u53c2\u6570\u6307\u5b9a\u5177\u4f53\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u3001pandas\u548cNumPy\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u8f6c\u6362\u548c\u5904\u7406\uff0c\u4e3a\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5efa\u6a21\u6253\u4e0b\u575a\u5b9e\u7684\u57fa\u7840\u3002\u65e0\u8bba\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u8fd8\u662f\u590d\u6742\u6570\u636e\u6846\u548c\u6570\u7ec4\u7684\u8f6c\u6362\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u5e2e\u52a9\u7528\u6237\u5e94\u5bf9\u5404\u79cd\u6570\u636e\u5904\u7406\u6311\u6218\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6216<code>zip()<\/code>\u51fd\u6570\u6765\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u952e\u5217\u8868\u548c\u4e00\u4e2a\u503c\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\nvalues = [1, 2, 3]\nresult_dict = dict(zip(keys, values))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u5b57\u5178<code>{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/code>\uff0c\u5176\u4e2d\u952e\u4e0e\u5bf9\u5e94\u7684\u503c\u4e00\u4e00\u5bf9\u5e94\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\uff1f<\/strong><br \/>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002\u901a\u8fc7<code>strptime()<\/code>\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ndate_string = &quot;2023-10-01&quot;\ndate_object = datetime.strptime(date_string, &quot;%Y-%m-%d&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f7f\u7528\u5b83\u8fdb\u884c\u65e5\u671f\u8ba1\u7b97\u6216\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u6539\u53d8\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u51fd\u6570\uff0c\u5982<code>int()<\/code>, <code>float()<\/code>, \u548c<code>str()<\/code>\u6765\u6539\u53d8\u6570\u636e\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-python\">num_str = &quot;42&quot;\nnum_int = int(num_str)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u628a\u5b57\u7b26\u4e32&quot;42&quot;\u8f6c\u6362\u4e3a\u6574\u657042\u3002\u5bf9\u4e8e\u6d6e\u70b9\u6570\u548c\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\u4e5f\u6709\u7c7b\u4f3c\u7684\u65b9\u6cd5\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5904\u7406\u5404\u79cd\u6570\u636e\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8f6c\u6362\u6570\u636e\u5f62\u5f0f\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8f6c\u6362\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u4f7f\u7528pandas\u5e93\u8f6c\u6362\u6570\u636e\u6846\u683c\u5f0f\u3001\u4f7f\u7528Nu [&hellip;]","protected":false},"author":3,"featured_media":1001317,"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\/1001306"}],"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=1001306"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001306\/revisions"}],"predecessor-version":[{"id":1001322,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001306\/revisions\/1001322"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001317"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}