{"id":1060736,"date":"2024-12-31T15:36:36","date_gmt":"2024-12-31T07:36:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1060736.html"},"modified":"2024-12-31T15:36:38","modified_gmt":"2024-12-31T07:36:38","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e9%83%a8%e5%88%86%e5%b1%9e%e6%80%a7%e5%81%9a%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1060736.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u90e8\u5206\u5c5e\u6027\u505a\u8f6c\u6362"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/7009c6b7-5060-4bbe-946f-0dbfe742b27b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5bf9\u90e8\u5206\u5c5e\u6027\u505a\u8f6c\u6362\" \/><\/p>\n<p><p> <strong>Python\u5bf9\u90e8\u5206\u5c5e\u6027\u505a\u8f6c\u6362<\/strong>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u5982<code>map<\/code>\u3001<code>filter<\/code>\u3001<code>lambda<\/code>\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\uff0c\u6765\u5bf9\u5bf9\u8c61\u6216\u6570\u636e\u7ed3\u6784\u7684\u90e8\u5206\u5c5e\u6027\u8fdb\u884c\u8f6c\u6362\u3002<strong>\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3001\u4f7f\u7528\u7c7b\u65b9\u6cd5\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982Pandas\u8fdb\u884c\u8f6c\u6362<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u90e8\u5206\u5c5e\u6027\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u5c5e\u6027\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5bf9\u5bf9\u8c61\u7684\u5c5e\u6027\u8fdb\u884c\u5404\u79cd\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5bf9\u5b57\u5178\u4e2d\u7684\u67d0\u4e9b\u952e\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_attributes(data):<\/p>\n<p>    def convert(value):<\/p>\n<p>        # \u8fd9\u91cc\u53ef\u4ee5\u8fdb\u884c\u4f60\u9700\u8981\u7684\u8f6c\u6362<\/p>\n<p>        return value.upper() if isinstance(value, str) else value<\/p>\n<p>    return {k: convert(v) for k, v in data.items()}<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>converted_data = convert_attributes(data)<\/p>\n<p>print(converted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>convert_attributes<\/code> \u51fd\u6570\u5bf9\u4f20\u5165\u5b57\u5178\u7684\u6bcf\u4e2a\u503c\u8fdb\u884c\u68c0\u67e5\u5e76\u8f6c\u6362\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7c7b\u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728\u5904\u7406\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u5728\u7c7b\u4e2d\u5b9a\u4e49\u65b9\u6cd5\u6765\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age, city):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.city = city<\/p>\n<p>    def convert_attributes(self):<\/p>\n<p>        self.name = self.name.upper()<\/p>\n<p>        self.city = self.city.upper()<\/p>\n<p>person = Person(&#39;Alice&#39;, 25, &#39;New York&#39;)<\/p>\n<p>person.convert_attributes()<\/p>\n<p>print(person.name, person.city)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>convert_attributes<\/code> \u65b9\u6cd5\u5c06 <code>name<\/code> \u548c <code>city<\/code> \u5c5e\u6027\u8f6c\u6362\u4e3a\u5927\u5199\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u8fdb\u884c\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5e38\u7528\u4e8e\u5bf9\u5927\u89c4\u6a21\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Pandas\u5bf9\u90e8\u5206\u5c5e\u6027\u8fdb\u884c\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;age&#39;: [25, 30, 35], &#39;city&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8f6c\u6362\u90e8\u5206\u5217<\/strong><\/h2>\n<p>df[&#39;name&#39;] = df[&#39;name&#39;].str.upper()<\/p>\n<p>df[&#39;city&#39;] = df[&#39;city&#39;].str.upper()<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cPandas DataFrame\u7684 <code>str.upper<\/code> \u65b9\u6cd5\u5c06 <code>name<\/code> \u548c <code>city<\/code> \u5217\u8f6c\u6362\u4e3a\u5927\u5199\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548cmap\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>map<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u90e8\u5206\u5c5e\u6027\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;},<\/p>\n<p>        {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;Los Angeles&#39;},<\/p>\n<p>        {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35, &#39;city&#39;: &#39;Chicago&#39;}]<\/p>\n<h2><strong>\u4f7f\u7528map\u51fd\u6570<\/strong><\/h2>\n<p>def convert_name(person):<\/p>\n<p>    person[&#39;name&#39;] = person[&#39;name&#39;].upper()<\/p>\n<p>    return person<\/p>\n<p>converted_data = list(map(convert_name, data))<\/p>\n<p>print(converted_data)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>converted_data = [{&#39;name&#39;: person[&#39;name&#39;].upper(), &#39;age&#39;: person[&#39;age&#39;], &#39;city&#39;: person[&#39;city&#39;].upper()} for person in data]<\/p>\n<p>print(converted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9\u5217\u8868\u4e2d\u7684\u5b57\u5178\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548cfilter\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c<code>filter<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5bf9\u90e8\u5206\u5c5e\u6027\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;},<\/p>\n<p>        {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;Los Angeles&#39;},<\/p>\n<p>        {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35, &#39;city&#39;: &#39;Chicago&#39;}]<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>def convert_person(person):<\/p>\n<p>    person[&#39;name&#39;] = person[&#39;name&#39;].upper()<\/p>\n<p>    return person<\/p>\n<p>converted_data = (convert_person(person) for person in data)<\/p>\n<p>print(list(converted_data))<\/p>\n<h2><strong>\u4f7f\u7528filter\u51fd\u6570<\/strong><\/h2>\n<p>def is_from_new_york(person):<\/p>\n<p>    return person[&#39;city&#39;] == &#39;New York&#39;<\/p>\n<p>filtered_data = filter(is_from_new_york, data)<\/p>\n<p>print(list(filtered_data))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c<code>filter<\/code>\u51fd\u6570\u5206\u522b\u5bf9\u90e8\u5206\u5c5e\u6027\u8fdb\u884c\u8f6c\u6362\u548c\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408\u591a\u4e2a\u65b9\u6cd5\u8fdb\u884c\u590d\u6742\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u7ed3\u5408\u81ea\u5b9a\u4e49\u51fd\u6570\u548cPandas\u8fdb\u884c\u590d\u6742\u6570\u636e\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def custom_conversion(value):<\/p>\n<p>    if isinstance(value, str):<\/p>\n<p>        return value.lower()<\/p>\n<p>    elif isinstance(value, int) and value &gt; 30:<\/p>\n<p>        return &#39;senior&#39;<\/p>\n<p>    return value<\/p>\n<p>data = {&#39;name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;age&#39;: [25, 30, 35], &#39;city&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528applymap\u8fdb\u884c\u590d\u6742\u8f6c\u6362<\/strong><\/h2>\n<p>df = df.applymap(custom_conversion)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>custom_conversion<\/code> \u51fd\u6570\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5c0f\u5199\u8f6c\u6362\uff0c\u5e76\u5c06\u5e74\u9f84\u5927\u4e8e30\u7684\u503c\u8f6c\u6362\u4e3a&#39;senior&#39;\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5728\u6570\u636e\u6d41\u4e2d\u8fdb\u884c\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u8f6c\u6362\u901a\u5e38\u662f\u6570\u636e\u5904\u7406\u6d41\u6c34\u7ebf\u7684\u4e00\u90e8\u5206\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u6570\u636e\u6d41\u4e2d\u4f7f\u7528\u8f6c\u6362\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def preprocess_data(df):<\/p>\n<p>    df[&#39;name&#39;] = df[&#39;name&#39;].str.upper()<\/p>\n<p>    df[&#39;age&#39;] = df[&#39;age&#39;].apply(lambda x: &#39;senior&#39; if x &gt; 30 else &#39;junior&#39;)<\/p>\n<p>    df[&#39;city&#39;] = df[&#39;city&#39;].str.lower()<\/p>\n<p>    return df<\/p>\n<p>data = {&#39;name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;age&#39;: [25, 30, 35], &#39;city&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>processed_df = preprocess_data(df)<\/p>\n<p>print(processed_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>preprocess_data<\/code> \u51fd\u6570\u5bf9DataFrame\u7684\u591a\u4e2a\u5217\u8fdb\u884c\u8f6c\u6362\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5728\u6570\u636e\u5904\u7406\u6d41\u6c34\u7ebf\u4e2d\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u9ad8\u7ea7\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Pandas\uff0cPython\u7684\u751f\u6001\u7cfb\u7edf\u4e2d\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8fdb\u884c\u9ad8\u7ea7\u6570\u636e\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<code>marshmallow<\/code>\u8fdb\u884c\u6570\u636e\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from marshmallow import Schema, fields, post_load<\/p>\n<p>class PersonSchema(Schema):<\/p>\n<p>    name = fields.Str()<\/p>\n<p>    age = fields.Int()<\/p>\n<p>    city = fields.Str()<\/p>\n<p>    @post_load<\/p>\n<p>    def convert_name(self, data, kwargs):<\/p>\n<p>        data[&#39;name&#39;] = data[&#39;name&#39;].upper()<\/p>\n<p>        return data<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>schema = PersonSchema()<\/p>\n<p>result = schema.load(data)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>marshmallow<\/code>\u5e93\u7684<code>post_load<\/code>\u65b9\u6cd5\u5728\u6570\u636e\u52a0\u8f7d\u540e\u5bf9<code>name<\/code>\u5b57\u6bb5\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u548c\u5de5\u5177\u6765\u5bf9\u5bf9\u8c61\u6216\u6570\u636e\u7ed3\u6784\u7684\u90e8\u5206\u5c5e\u6027\u8fdb\u884c\u8f6c\u6362\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u7c7b\u65b9\u6cd5\u3001Pandas\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u3001\u7b2c\u4e09\u65b9\u5e93\u7b49\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7075\u6d3b\u548c\u9ad8\u6548\u7684\u6570\u636e\u8f6c\u6362\u3002\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5b9e\u73b0\u4ee3\u7801\u590d\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u4ee3\u7801\u590d\u7528\u662f\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u7684\u91cd\u8981\u624b\u6bb5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c06\u8f6c\u6362\u903b\u8f91\u5c01\u88c5\u4e3a\u53ef\u590d\u7528\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># converter.py<\/p>\n<p>def convert_to_upper(value):<\/p>\n<p>    return value.upper() if isinstance(value, str) else value<\/p>\n<p>def convert_age(value):<\/p>\n<p>    return &#39;senior&#39; if value &gt; 30 else &#39;junior&#39;<\/p>\n<h2><strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<p>import pandas as pd<\/p>\n<p>from converter import convert_to_upper, convert_age<\/p>\n<p>data = {&#39;name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;age&#39;: [25, 30, 35], &#39;city&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&#39;name&#39;] = df[&#39;name&#39;].apply(convert_to_upper)<\/p>\n<p>df[&#39;age&#39;] = df[&#39;age&#39;].apply(convert_age)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8f6c\u6362\u903b\u8f91\u88ab\u5c01\u88c5\u5728<code>converter.py<\/code>\u6a21\u5757\u4e2d\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u590d\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u5bf9\u90e8\u5206\u5c5e\u6027\u8fdb\u884c\u8f6c\u6362\uff0c\u6ee1\u8db3\u5404\u79cd\u6570\u636e\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9009\u62e9\u6027\u5730\u8f6c\u6362\u5bf9\u8c61\u7684\u5c5e\u6027\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u81ea\u5b9a\u4e49\u65b9\u6cd5\u6216\u4f7f\u7528\u7c7b\u7684<code>__dict__<\/code>\u5c5e\u6027\u6765\u9009\u62e9\u6027\u5730\u8f6c\u6362\u5bf9\u8c61\u7684\u67d0\u4e9b\u5c5e\u6027\u3002\u53ef\u4ee5\u904d\u5386\u5bf9\u8c61\u7684\u5c5e\u6027\u5b57\u5178\uff0c\u68c0\u67e5\u6bcf\u4e2a\u5c5e\u6027\u7684\u540d\u79f0\u548c\u7c7b\u578b\uff0c\u7136\u540e\u6839\u636e\u9700\u8981\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5e0c\u671b\u5c06\u67d0\u4e2a\u5b57\u7b26\u4e32\u5c5e\u6027\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5c5e\u6027\u8f6c\u6362\u65b9\u6cd5\uff1f<\/strong><br \/>\u5e38\u7528\u7684\u5c5e\u6027\u8f6c\u6362\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\uff08\u5982<code>int()<\/code>\u3001<code>float()<\/code>\u3001<code>str()<\/code>\u7b49\uff09\u8fdb\u884c\u57fa\u672c\u7c7b\u578b\u8f6c\u6362\uff0c\u6216\u8005\u4f7f\u7528\u5e93\u51fd\u6570\uff08\u5982<code>datetime.strptime()<\/code>\u8fdb\u884c\u65e5\u671f\u8f6c\u6362\uff09\u3002\u8fd8\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u8f6c\u6362\u51fd\u6570\u6765\u5904\u7406\u590d\u6742\u7684\u8f6c\u6362\u903b\u8f91\uff0c\u4f8b\u5982\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6570\u636e\u6846\u4e2d\u5bf9\u7279\u5b9a\u5217\u8fdb\u884c\u5c5e\u6027\u8f6c\u6362\uff1f<\/strong><br \/>\u5982\u679c\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e\u6846\uff0c\u53ef\u4ee5\u901a\u8fc7\u9009\u62e9\u7279\u5b9a\u5217\u5e76\u5e94\u7528\u8f6c\u6362\u51fd\u6570\u6765\u8fdb\u884c\u5c5e\u6027\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>dataframe[&#39;column_name&#39;] = dataframe[&#39;column_name&#39;].apply(some_conversion_function)<\/code>\u7684\u65b9\u5f0f\uff0c\u5bf9\u7279\u5b9a\u5217\u8fdb\u884c\u8f6c\u6362\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>astype()<\/code>\u65b9\u6cd5\u5feb\u901f\u8f6c\u6362\u6570\u636e\u7c7b\u578b\uff0c\u5982<code>dataframe[&#39;column_name&#39;] = dataframe[&#39;column_name&#39;].astype(int)<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bf9\u90e8\u5206\u5c5e\u6027\u505a\u8f6c\u6362\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u5982map\u3001filter\u3001lambda\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1060742,"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\/1060736"}],"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=1060736"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060736\/revisions"}],"predecessor-version":[{"id":1060743,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060736\/revisions\/1060743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1060742"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1060736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1060736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1060736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}