{"id":1151561,"date":"2025-01-13T17:15:01","date_gmt":"2025-01-13T09:15:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1151561.html"},"modified":"2025-01-13T17:15:03","modified_gmt":"2025-01-13T09:15:03","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2object%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1151561.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u6362object\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181755\/531efada-3c25-453b-b730-7d7c2fc973e6.webp\" alt=\"python\u5982\u4f55\u8f6c\u6362object\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06object\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001Pandas\u5e93\u4e2d\u7684\u65b9\u6cd5\u3001Numpy\u5e93\u4e2d\u7684\u65b9\u6cd5\u7b49\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\u3001\u4f7f\u7528Pandas\u7684<code>astype<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528Numpy\u7684<code>astype<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u3002\u8fd9\u91cc\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528Pandas\u7684<code>astype<\/code>\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>Pandas\u5e93\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c<code>astype<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u7c7b\u578b\u3002\u4f7f\u7528\u8be5\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06DataFrame\u4e2d\u7684object\u7c7b\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\uff0c\u5982int\u3001float\u3001datetime\u7b49\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7c7b\u578b\u8f6c\u6362\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u4e86\u591a\u79cd\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\uff0c\u5982<code>int()<\/code>, <code>float()<\/code>, <code>str()<\/code>, <code>list()<\/code>, <code>tuple()<\/code>\u7b49\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5c06\u6570\u636e\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">string_num = &quot;123&quot;<\/p>\n<p>int_num = int(string_num)<\/p>\n<p>print(int_num)  # \u8f93\u51fa: 123<\/p>\n<p>print(type(int_num))  # \u8f93\u51fa: &lt;class &#39;int&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">string_num = &quot;123.45&quot;<\/p>\n<p>float_num = float(string_num)<\/p>\n<p>print(float_num)  # \u8f93\u51fa: 123.45<\/p>\n<p>print(type(float_num))  # \u8f93\u51fa: &lt;class &#39;float&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u7684<code>astype<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u5f3a\u5927\u5e93\uff0c<code>astype<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06DataFrame\u6216Series\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5c06DataFrame\u4e2d\u7684\u5217\u8f6c\u6362\u4e3a\u6574\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;col1&#39;: [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;], &#39;col2&#39;: [&#39;4&#39;, &#39;5&#39;, &#39;6&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&#39;col1&#39;] = df[&#39;col1&#39;].astype(int)<\/p>\n<p>print(df)<\/p>\n<p>print(df.dtypes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u5c06DataFrame\u4e2d\u7684\u5217\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;col2&#39;] = df[&#39;col2&#39;].astype(float)<\/p>\n<p>print(df)<\/p>\n<p>print(df.dtypes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Numpy\u7684<code>astype<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Numpy\u662f\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c<code>astype<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06Numpy\u6570\u7ec4\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5c06Numpy\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([&#39;1&#39;, &#39;2&#39;, &#39;3&#39;])<\/p>\n<p>int_array = array.astype(int)<\/p>\n<p>print(int_array)<\/p>\n<p>print(int_array.dtype)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u5c06Numpy\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">float_array = array.astype(float)<\/p>\n<p>print(float_array)<\/p>\n<p>print(float_array.dtype)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u6839\u636e\u7279\u5b9a\u89c4\u5219\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u81ea\u5b9a\u4e49\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def str_to_int(s):<\/p>\n<p>    try:<\/p>\n<p>        return int(s)<\/p>\n<p>    except ValueError:<\/p>\n<p>        return None<\/p>\n<p>string_list = [&#39;1&#39;, &#39;2&#39;, &#39;three&#39;, &#39;4&#39;]<\/p>\n<p>int_list = [str_to_int(x) for x in string_list]<\/p>\n<p>print(int_list)  # \u8f93\u51fa: [1, 2, None, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u81ea\u5b9a\u4e49\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def str_to_float(s):<\/p>\n<p>    try:<\/p>\n<p>        return float(s)<\/p>\n<p>    except ValueError:<\/p>\n<p>        return None<\/p>\n<p>string_list = [&#39;1.1&#39;, &#39;2.2&#39;, &#39;three&#39;, &#39;4.4&#39;]<\/p>\n<p>float_list = [str_to_float(x) for x in string_list]<\/p>\n<p>print(float_list)  # \u8f93\u51fa: [1.1, 2.2, None, 4.4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5\u548c\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7279\u6b8a\u60c5\u51b5\u548c\u9519\u8bef\uff0c\u9700\u8981\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u5904\u7406\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f3a\u5931\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>fillna<\/code>\u65b9\u6cd5\u586b\u5145\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.DataFrame({&#39;col1&#39;: [&#39;1&#39;, &#39;2&#39;, None, &#39;4&#39;]})<\/p>\n<p>df[&#39;col1&#39;] = df[&#39;col1&#39;].fillna(&#39;0&#39;).astype(int)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u5904\u7406\u65e0\u6cd5\u8f6c\u6362\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6cd5\u8f6c\u6362\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_convert(s, target_type):<\/p>\n<p>    try:<\/p>\n<p>        return target_type(s)<\/p>\n<p>    except (ValueError, TypeError):<\/p>\n<p>        return None<\/p>\n<p>string_list = [&#39;1&#39;, &#39;2&#39;, &#39;three&#39;, &#39;4&#39;]<\/p>\n<p>int_list = [safe_convert(x, int) for x in string_list]<\/p>\n<p>print(int_list)  # \u8f93\u51fa: [1, 2, None, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8f6c\u6362object\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\u3001Pandas\u5e93\u4e2d\u7684<code>astype<\/code>\u65b9\u6cd5\u3001Numpy\u5e93\u4e2d\u7684<code>astype<\/code>\u65b9\u6cd5\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3002\u901a\u8fc7\u5408\u7406\u5904\u7406\u7f3a\u5931\u503c\u548c\u65e0\u6cd5\u8f6c\u6362\u7684\u503c\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u8f6c\u6362\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06Object\u6570\u636e\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u8f6c\u6362\u51fd\u6570\u6765\u5c06Object\u6570\u636e\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u5c06Object\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f7f\u7528<code>str()<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528<code>float()<\/code>\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u9700\u8981\u786e\u4fddObject\u7684\u5185\u5bb9\u662f\u53ef\u4ee5\u88ab\u8f6c\u6362\u7684\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u4f7f\u7528<code>astype()<\/code>\u51fd\u6570\u65f6\uff0cPandas\u5e93\u4e5f\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p><strong>\u4f7f\u7528Pandas\u5e93\u5982\u4f55\u5904\u7406Object\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\uff1f<\/strong><br \/>Pandas\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406DataFrame\u4e2d\u7684Object\u7c7b\u578b\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>apply()<\/code>\u65b9\u6cd5\u7ed3\u5408\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8f6c\u6362Object\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528<code>astype()<\/code>\u65b9\u6cd5\u76f4\u63a5\u5c06\u5217\u8f6c\u6362\u4e3a\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>pd.to_numeric()<\/code>\u548c<code>pd.to_datetime()<\/code>\u7b49\u51fd\u6570\u53ef\u4ee5\u5c06Object\u8f6c\u6362\u4e3a\u6570\u5b57\u6216\u65e5\u671f\u683c\u5f0f\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362Object\u6570\u636e\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\u6216\u5f02\u5e38\u6570\u636e\uff1f<\/strong><br \/>\u5728\u8f6c\u6362Object\u6570\u636e\u65f6\uff0c\u7f3a\u5931\u503c\u548c\u5f02\u5e38\u6570\u636e\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u4e2d\u7684<code>dropna()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\uff0c\u6216\u8005\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u66ff\u6362\u7f3a\u5931\u503c\u4ee5\u786e\u4fdd\u8f6c\u6362\u987a\u5229\u8fdb\u884c\u3002\u5bf9\u4e8e\u5f02\u5e38\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u66ff\u6362\u4e0d\u7b26\u5408\u9884\u671f\u7684\u503c\uff0c\u6216\u8005\u4f7f\u7528\u6761\u4ef6\u7b5b\u9009\u6765\u5904\u7406\u8fd9\u4e9b\u6570\u636e\uff0c\u4ece\u800c\u4fdd\u8bc1\u6570\u636e\u7684\u8d28\u91cf\u548c\u5b8c\u6574\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06object\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001Pandas\u5e93\u4e2d\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1151565,"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\/1151561"}],"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=1151561"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151561\/revisions"}],"predecessor-version":[{"id":1151566,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151561\/revisions\/1151566"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1151565"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1151561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1151561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1151561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}