{"id":1091448,"date":"2025-01-08T14:11:47","date_gmt":"2025-01-08T06:11:47","guid":{"rendered":""},"modified":"2025-01-08T14:11:49","modified_gmt":"2025-01-08T06:11:49","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bb%99%e4%b8%80%e5%88%97%e8%b5%8b%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1091448.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7ed9\u4e00\u5217\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203615\/36892760-4a80-4edb-9d43-5095dbec6eaf.webp\" alt=\"python\u4e2d\u5982\u4f55\u7ed9\u4e00\u5217\u8d4b\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u7ed9\u4e00\u5217\u8d4b\u503c\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528\u5217\u8868\u6216\u6570\u7ec4\u3001\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528apply\u51fd\u6570\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7Pandas\u5e93\u76f4\u63a5\u5bf9DataFrame\u8fdb\u884c\u8d4b\u503c\u3002<\/strong> Pandas\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u4f7f\u5f97\u6570\u636e\u5904\u7406\u53d8\u5f97\u975e\u5e38\u65b9\u4fbf\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u7ed9\u4e00\u5217\u8d4b\u503c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528Pandas\u5e93\u8fdb\u884c\u8d4b\u503c<\/strong><\/h2>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2aDataFrame\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5bf9\u5176\u4e2d\u7684\u4e00\u5217\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u521b\u5efaDataFrame<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aDataFrame\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u4e2d\u7684DataFrame\u51fd\u6570\u6765\u521b\u5efa\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]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B<\/p>\n<p>0  1  4<\/p>\n<p>1  2  5<\/p>\n<p>2  3  6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u76f4\u63a5\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5217\u540d\u5bf9DataFrame\u4e2d\u7684\u4e00\u5217\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;C&#39;] = [7, 8, 9]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  C<\/p>\n<p>0  1  4  7<\/p>\n<p>1  2  5  8<\/p>\n<p>2  3  6  9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u65b0\u589e\u4e86\u4e00\u5217&#39;C&#39;\uff0c\u5e76\u4e3a\u5176\u8d4b\u503c\u4e3a[7, 8, 9]\u3002<\/p>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528apply\u51fd\u6570<\/h2>\n<\/p>\n<p><p>apply\u51fd\u6570\u53ef\u4ee5\u5bf9DataFrame\u4e2d\u7684\u6bcf\u4e00\u884c\u6216\u6bcf\u4e00\u5217\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;D&#39;] = df.apply(lambda row: row[&#39;A&#39;] + row[&#39;B&#39;], axis=1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  C   D<\/p>\n<p>0  1  4  7   5<\/p>\n<p>1  2  5  8   7<\/p>\n<p>2  3  6  9   9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u65b0\u589e\u4e86\u4e00\u5217&#39;D&#39;\uff0c\u5e76\u4e3a\u5176\u8d4b\u503c\u4e3a&#39;A&#39;\u548c&#39;B&#39;\u4e24\u5217\u4e4b\u548c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u6216\u6570\u7ec4\u8fdb\u884c\u8d4b\u503c<\/strong><\/h2>\n<p><p>\u9664\u4e86\u4f7f\u7528Pandas\u5e93\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6216\u6570\u7ec4\u5bf9\u4e00\u5217\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528\u5217\u8868\u8d4b\u503c<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5217\u8868<\/p>\n<p>lst = [10, 11, 12]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8d4b\u503c\u7ed9DataFrame\u4e2d\u7684\u4e00\u5217<\/strong><\/h2>\n<p>df[&#39;E&#39;] = lst<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  C   D   E<\/p>\n<p>0  1  4  7   5  10<\/p>\n<p>1  2  5  8   7  11<\/p>\n<p>2  3  6  9   9  12<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528NumPy\u6570\u7ec4\u8d4b\u503c<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([13, 14, 15])<\/p>\n<h2><strong>\u5c06NumPy\u6570\u7ec4\u8d4b\u503c\u7ed9DataFrame\u4e2d\u7684\u4e00\u5217<\/strong><\/h2>\n<p>df[&#39;F&#39;] = arr<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  C   D   E   F<\/p>\n<p>0  1  4  7   5  10  13<\/p>\n<p>1  2  5  8   7  11  14<\/p>\n<p>2  3  6  9   9  12  15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u6570\u7ec4\u5bf9DataFrame\u4e2d\u7684\u4e00\u5217\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u6761\u4ef6\u8d4b\u503c<\/strong><\/h2>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u6765\u5bf9\u4e00\u5217\u8fdb\u884c\u8d4b\u503c\u3002Pandas\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u6839\u636e\u6761\u4ef6\u8d4b\u503c<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6839\u636e\u6761\u4ef6\u5bf9\u4e00\u5217\u8fdb\u884c\u8d4b\u503c<\/p>\n<p>df[&#39;G&#39;] = df[&#39;A&#39;].apply(lambda x: &#39;High&#39; if x &gt; 2 else &#39;Low&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  C   D   E   F     G<\/p>\n<p>0  1  4  7   5  10  13   Low<\/p>\n<p>1  2  5  8   7  11  14   Low<\/p>\n<p>2  3  6  9   9  12  15  High<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6839\u636e\u5217&#39;A&#39;\u7684\u503c\u6765\u5bf9\u65b0\u5217&#39;G&#39;\u8fdb\u884c\u8d4b\u503c\uff0c\u5982\u679c&#39;A&#39;\u7684\u503c\u5927\u4e8e2\uff0c\u5219\u8d4b\u503c\u4e3a&#39;High&#39;\uff0c\u5426\u5219\u8d4b\u503c\u4e3a&#39;Low&#39;\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528loc\u65b9\u6cd5\u8fdb\u884c\u6761\u4ef6\u8d4b\u503c<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528loc\u65b9\u6cd5\u6839\u636e\u6761\u4ef6\u5bf9\u4e00\u5217\u8fdb\u884c\u8d4b\u503c<\/p>\n<p>df.loc[df[&#39;A&#39;] &gt; 2, &#39;H&#39;] = &#39;Pass&#39;<\/p>\n<p>df.loc[df[&#39;A&#39;] &lt;= 2, &#39;H&#39;] = &#39;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&#39;<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  C   D   E   F     G     H<\/p>\n<p>0  1  4  7   5  10  13   Low  Fail<\/p>\n<p>1  2  5  8   7  11  14   Low  Fail<\/p>\n<p>2  3  6  9   9  12  15  High  Pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528loc\u65b9\u6cd5\u6839\u636e\u5217&#39;A&#39;\u7684\u503c\u6765\u5bf9\u65b0\u5217&#39;H&#39;\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528\u51fd\u6570\u8fdb\u884c\u8d4b\u503c<\/strong><\/h2>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u4e00\u5217\u8fdb\u884c\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u5b9a\u4e49\u51fd\u6570\u5e76\u5e94\u7528<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<\/p>\n<p>def calculate_value(row):<\/p>\n<p>    return row[&#39;A&#39;] * row[&#39;B&#39;]<\/p>\n<h2><strong>\u5c06\u51fd\u6570\u5e94\u7528\u5230DataFrame\u4e2d\u7684\u4e00\u5217<\/strong><\/h2>\n<p>df[&#39;I&#39;] = df.apply(calculate_value, axis=1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  C   D   E   F     G     H   I<\/p>\n<p>0  1  4  7   5  10  13   Low  Fail   4<\/p>\n<p>1  2  5  8   7  11  14   Low  Fail  10<\/p>\n<p>2  3  6  9   9  12  15  High  Pass  18<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570calculate_value\uff0c\u5e76\u4f7f\u7528apply\u65b9\u6cd5\u5c06\u5176\u5e94\u7528\u5230DataFrame\u4e2d\u7684\u4e00\u5217\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528\u63d2\u503c\u65b9\u6cd5\u8fdb\u884c\u8d4b\u503c<\/strong><\/h2>\n<p><p>\u6709\u65f6\u5019\uff0c\u6570\u636e\u4e2d\u4f1a\u6709\u7f3a\u5931\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u63d2\u503c\u65b9\u6cd5\u6765\u5bf9\u7f3a\u5931\u503c\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528fillna\u65b9\u6cd5<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5f15\u5165\u7f3a\u5931\u503c<\/p>\n<p>df.loc[1, &#39;A&#39;] = None<\/p>\n<h2><strong>\u4f7f\u7528fillna\u65b9\u6cd5\u5bf9\u7f3a\u5931\u503c\u8fdb\u884c\u8d4b\u503c<\/strong><\/h2>\n<p>df[&#39;A&#39;] = df[&#39;A&#39;].fillna(df[&#39;A&#39;].mean())<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>     A  B  C   D   E   F     G     H   I<\/p>\n<p>0  1.0  4  7   5  10  13   Low  Fail   4<\/p>\n<p>1  2.0  5  8   7  11  14   Low  Fail  10<\/p>\n<p>2  3.0  6  9   9  12  15  High  Pass  18<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528fillna\u65b9\u6cd5\u5bf9\u7f3a\u5931\u503c\u8fdb\u884c\u4e86\u8d4b\u503c\uff0c\u4f7f\u7528\u7684\u662f\u5217&#39;A&#39;\u7684\u5747\u503c\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528interpolate\u65b9\u6cd5<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5f15\u5165\u66f4\u591a\u7f3a\u5931\u503c<\/p>\n<p>df.loc[0, &#39;B&#39;] = None<\/p>\n<p>df.loc[2, &#39;B&#39;] = None<\/p>\n<h2><strong>\u4f7f\u7528interpolate\u65b9\u6cd5\u5bf9\u7f3a\u5931\u503c\u8fdb\u884c\u63d2\u503c<\/strong><\/h2>\n<p>df[&#39;B&#39;] = df[&#39;B&#39;].interpolate()<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>     A    B  C   D   E   F     G     H   I<\/p>\n<p>0  1.0  4.5  7   5  10  13   Low  Fail   4<\/p>\n<p>1  2.0  5.0  8   7  11  14   Low  Fail  10<\/p>\n<p>2  3.0  5.5  9   9  12  15  High  Pass  18<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528interpolate\u65b9\u6cd5\u5bf9\u7f3a\u5931\u503c\u8fdb\u884c\u4e86\u63d2\u503c\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528map\u65b9\u6cd5\u8fdb\u884c\u8d4b\u503c<\/strong><\/h2>\n<p><p>map\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u5230DataFrame\u4e2d\u7684\u4e00\u5217\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6620\u5c04\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528map\u65b9\u6cd5<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u6620\u5c04\u5b57\u5178<\/p>\n<p>mapping = {4: &#39;Four&#39;, 5: &#39;Five&#39;, 6: &#39;Six&#39;}<\/p>\n<h2><strong>\u4f7f\u7528map\u65b9\u6cd5\u8fdb\u884c\u8d4b\u503c<\/strong><\/h2>\n<p>df[&#39;J&#39;] = df[&#39;B&#39;].map(mapping)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>     A    B  C   D   E   F     G     H   I     J<\/p>\n<p>0  1.0  4.5  7   5  10  13   Low  Fail   4   NaN<\/p>\n<p>1  2.0  5.0  8   7  11  14   Low  Fail  10  Five<\/p>\n<p>2  3.0  5.5  9   9  12  15  High  Pass  18   NaN<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528map\u65b9\u6cd5\u5c06\u5217&#39;B&#39;\u4e2d\u7684\u503c\u6620\u5c04\u5230\u65b0\u5217&#39;J&#39;\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u7ed9\u4e00\u5217\u8d4b\u503c\u7684\u65b9\u6cd5\u975e\u5e38\u591a\u6837\u5316\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528apply\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u6216\u6570\u7ec4\u3001\u4f7f\u7528\u6761\u4ef6\u8d4b\u503c\u3001\u4f7f\u7528\u51fd\u6570\u8fdb\u884c\u8d4b\u503c\u3001\u4f7f\u7528\u63d2\u503c\u65b9\u6cd5\u3001\u4f7f\u7528map\u65b9\u6cd5\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u548c\u5904\u7406\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed9DataFrame\u7684\u4e00\u5217\u8d4b\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5982\u679c\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e\uff0c\u8d4b\u503c\u7ed9DataFrame\u7684\u4e00\u5217\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5217\u540d\u6765\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2aDataFrame <code>df<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>df[&#39;column_name&#39;] = value<\/code> \u7684\u65b9\u5f0f\u7ed9\u6307\u5b9a\u5217\u8d4b\u503c\u3002\u5982\u679c\u9700\u8981\u57fa\u4e8e\u67d0\u4e9b\u6761\u4ef6\u6765\u8d4b\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>df.loc<\/code> \u65b9\u6cd5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728\u7ed9\u5217\u8d4b\u503c\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728\u8d4b\u503c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f3a\u5931\u503c\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u7684 <code>fillna()<\/code> \u65b9\u6cd5\u6765\u586b\u8865\u7f3a\u5931\u503c\uff0c\u6216\u8005\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u6765\u51b3\u5b9a\u662f\u5426\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>df[&#39;column_name&#39;] = df[&#39;column_name&#39;].fillna(value)<\/code> \u6765\u66ff\u6362\u7f3a\u5931\u503c\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u6839\u636e\u5176\u4ed6\u5217\u7684\u503c\u6765\u4e3a\u4e00\u5217\u8d4b\u503c\uff1f<\/strong><br \/>\u901a\u8fc7Pandas\u7684 <code>apply()<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5176\u4ed6\u5217\u7684\u503c\u6765\u52a8\u6001\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u6839\u636e\u4e00\u884c\u4e2d\u7684\u591a\u4e2a\u5217\u7684\u503c\u6765\u8fd4\u56de\u65b0\u7684\u8d4b\u503c\uff0c\u7136\u540e\u4f7f\u7528 <code>df[&#39;new_column&#39;] = df.apply(your_function, axis=1)<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u6027\u9ad8\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u8d4b\u503c\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u7ed9\u4e00\u5217\u8d4b\u503c\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528\u5217\u8868\u6216\u6570\u7ec4\u3001\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1091455,"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\/1091448"}],"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=1091448"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091448\/revisions"}],"predecessor-version":[{"id":1091458,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091448\/revisions\/1091458"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1091455"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1091448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1091448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1091448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}