{"id":926535,"date":"2024-12-26T15:52:46","date_gmt":"2024-12-26T07:52:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/926535.html"},"modified":"2024-12-26T15:52:49","modified_gmt":"2024-12-26T07:52:49","slug":"python%e5%a6%82%e4%bd%95apply","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/926535.html","title":{"rendered":"python\u5982\u4f55apply"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063050\/2d99e274-0c9e-4906-a4bf-6e1bfcc8df31.webp\" alt=\"python\u5982\u4f55apply\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0capply\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u5728Pandas\u5e93\u4e2d\u5bf9\u6570\u636e\u6846\u7684\u884c\u6216\u5217\u5e94\u7528\u7279\u5b9a\u7684\u51fd\u6570\u3002apply\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u6279\u91cf\u5904\u7406\u3001\u6570\u636e\u8f6c\u6362\u3001\u6e05\u6d17\u6570\u636e\u7b49\u529f\u80fd\u3002\u4f7f\u7528apply\u51fd\u6570\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u8fd0\u884c\u6548\u7387\u3002<\/strong>\u5176\u4e2d\uff0c\u6279\u91cf\u5904\u7406\u662f\u6307\u901a\u8fc7\u4e00\u6b21\u64cd\u4f5c\u5bf9\u6570\u636e\u96c6\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001Pandas\u5e93\u4e2d\u7684apply\u51fd\u6570<\/h2>\n<\/p>\n<p><p>Pandas\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\u4e4b\u4e00\uff0c\u5176\u4e2d\u7684apply\u51fd\u6570\u7528\u4e8e\u5c06\u67d0\u4e2a\u51fd\u6570\u5e94\u7528\u5230\u6570\u636e\u6846\u6216\u7cfb\u5217\u7684\u884c\u6216\u5217\u4e0a\u3002\u901a\u8fc7apply\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u7075\u6d3b\u7684\u5904\u7406\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u6570\u636e\u6846\u7684\u884c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6846\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a<code>axis=1<\/code>\u6765\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u64cd\u4f5c\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5bf9\u6570\u636e\u6846\u7684\u6bcf\u4e00\u884c\u5e94\u7528\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6]<\/p>\n<p>})<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u5904\u7406\u884c\u6570\u636e<\/strong><\/h2>\n<p>def sum_row(row):<\/p>\n<p>    return row[&#39;A&#39;] + row[&#39;B&#39;]<\/p>\n<h2><strong>\u4f7f\u7528apply\u51fd\u6570\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u64cd\u4f5c<\/strong><\/h2>\n<p>df[&#39;Sum&#39;] = df.apply(sum_row, axis=1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0capply\u51fd\u6570\u901a\u8fc7axis=1\u5bf9\u6570\u636e\u6846\u7684\u6bcf\u4e00\u884c\u5e94\u7528\u4e86sum_row\u51fd\u6570\uff0c\u8ba1\u7b97\u4e86\u6bcf\u884c\u7684\u548c\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u6570\u636e\u6846\u7684\u5217\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5bf9\u6570\u636e\u6846\u7684\u6bcf\u4e00\u5217\u8fdb\u884c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5c06<code>axis<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a0\uff0c\u6216\u4e0d\u8bbe\u7f6e\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u60c5\u51b5\u4e0b<code>axis=0<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u5904\u7406\u5217\u6570\u636e<\/p>\n<p>def double_column(column):<\/p>\n<p>    return column * 2<\/p>\n<h2><strong>\u4f7f\u7528apply\u51fd\u6570\u5bf9\u6bcf\u4e00\u5217\u8fdb\u884c\u64cd\u4f5c<\/strong><\/h2>\n<p>df_doubled = df.apply(double_column)<\/p>\n<p>print(df_doubled)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0capply\u51fd\u6570\u5bf9\u6570\u636e\u6846\u7684\u6bcf\u4e00\u5217\u5e94\u7528\u4e86double_column\u51fd\u6570\uff0c\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u4e86\u7ffb\u500d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u4e0eapply\u7ed3\u5408\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408\u81ea\u5b9a\u4e49\u51fd\u6570\u548capply\u51fd\u6570\u6765\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5bf9\u6570\u636e\u8fdb\u884c\u7075\u6d3b\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u5e94\u7528\u6570\u5b66\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e9b\u6570\u5b66\u51fd\u6570\uff0c\u5e76\u901a\u8fc7apply\u51fd\u6570\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u6570\u636e\u6846\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u5e73\u65b9<\/p>\n<p>def square(x):<\/p>\n<p>    return x  2<\/p>\n<h2><strong>\u4f7f\u7528apply\u51fd\u6570\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97<\/strong><\/h2>\n<p>df_squared = df.applymap(square)<\/p>\n<p>print(df_squared)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u5e94\u7528\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528apply\u51fd\u6570\u7ed3\u5408\u81ea\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5c06\u6570\u636e\u6846\u4e2d\u6240\u6709\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u6570\u636e\u6846<\/p>\n<p>df_text = pd.DataFrame({<\/p>\n<p>    &#39;Text&#39;: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>})<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u8f6c\u6362\u4e3a\u5927\u5199<\/strong><\/h2>\n<p>def to_upper(text):<\/p>\n<p>    return text.upper()<\/p>\n<h2><strong>\u4f7f\u7528apply\u51fd\u6570\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u5927\u5199\u8f6c\u6362<\/strong><\/h2>\n<p>df_text[&#39;Upper&#39;] = df_text[&#39;Text&#39;].apply(to_upper)<\/p>\n<p>print(df_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001apply\u4e0elambda\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528lambda\u51fd\u6570\u6765\u7b80\u5316\u4ee3\u7801\u3002lambda\u51fd\u6570\u662f\u4e00\u79cd\u533f\u540d\u51fd\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u4f7f\u7528lambda\u8fdb\u884c\u7b80\u5355\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7lambda\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5728apply\u4e2d\u76f4\u63a5\u5199\u51fa\u7b80\u5355\u7684\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u5355\u72ec\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528lambda\u51fd\u6570\u8fdb\u884c\u7b80\u5355\u7684\u52a0\u6cd5\u64cd\u4f5c<\/p>\n<p>df[&#39;A_plus_10&#39;] = df[&#39;A&#39;].apply(lambda x: x + 10)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u4f7f\u7528lambda\u8fdb\u884c\u6761\u4ef6\u5224\u65ad<\/h3>\n<\/p>\n<p><p>lambda\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u4f8b\u5982\uff0c\u6839\u636e\u67d0\u4e2a\u6761\u4ef6\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528lambda\u51fd\u6570\u8fdb\u884c\u6761\u4ef6\u5224\u65ad<\/p>\n<p>df[&#39;Category&#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><h2>\u56db\u3001apply\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316<\/h2>\n<\/p>\n<p><p>\u867d\u7136apply\u51fd\u6570\u975e\u5e38\u7075\u6d3b\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u6210\u4e3a\u74f6\u9888\u3002\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u4f18\u5316\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u4f7f\u7528\u5411\u91cf\u5316\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bf9\u6570\u636e\u6846\u8fdb\u884c\u6279\u91cf\u5904\u7406\uff0c\u8fd9\u79cd\u65b9\u5f0f\u901a\u5e38\u6bd4apply\u51fd\u6570\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5411\u91cf\u5316\u64cd\u4f5c\u8fdb\u884c\u52a0\u6cd5<\/p>\n<p>df[&#39;A_plus_10_vectorized&#39;] = df[&#39;A&#39;] + 10<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u4f7f\u7528NumPy\u51fd\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u4e2d\u7684\u8bb8\u591a\u51fd\u6570\u90fd\u662f\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\u7684\uff0c\u53ef\u4ee5\u7528\u4e8e\u66ff\u4ee3apply\u51fd\u6570\u5b9e\u73b0\u67d0\u4e9b\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u51fd\u6570\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97<\/strong><\/h2>\n<p>df[&#39;A_squared&#39;] = np.square(df[&#39;A&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001apply\u51fd\u6570\u7684\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h2>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0capply\u51fd\u6570\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u6848\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u6570\u636e\u6e05\u6d17<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0capply\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u7f3a\u5931\u503c\u3001\u5f02\u5e38\u503c\u7b49\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5c06\u6570\u636e\u6846\u4e2d\u7684\u7f3a\u5931\u503c\u586b\u5145\u4e3a\u67d0\u4e2a\u56fa\u5b9a\u503c\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>df_nan = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, np.nan, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, np.nan]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u7528apply\u51fd\u6570\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df_filled = df_nan.apply(lambda x: x.fillna(0))<\/p>\n<p>print(df_filled)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 \u7279\u5f81\u5de5\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u7279\u5f81\u5de5\u7a0b\u8fc7\u7a0b\u4e2d\uff0capply\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u65b0\u7684\u7279\u5f81\u3002\u4f8b\u5982\uff0c\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0c\u5e76\u63d0\u53d6\u5e74\u4efd\u4f5c\u4e3a\u65b0\u7279\u5f81\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>df_dates = pd.DataFrame({<\/p>\n<p>    &#39;Date&#39;: [&#39;2021-01-01&#39;, &#39;2022-02-15&#39;, &#39;2023-03-20&#39;]<\/p>\n<p>})<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u63d0\u53d6\u5e74\u4efd<\/strong><\/h2>\n<p>def extract_year(date_str):<\/p>\n<p>    return pd.to_datetime(date_str).year<\/p>\n<h2><strong>\u4f7f\u7528apply\u51fd\u6570\u751f\u6210\u5e74\u4efd\u7279\u5f81<\/strong><\/h2>\n<p>df_dates[&#39;Year&#39;] = df_dates[&#39;Date&#39;].apply(extract_year)<\/p>\n<p>print(df_dates)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.3 \u6570\u636e\u805a\u5408<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u805a\u5408\u8fc7\u7a0b\u4e2d\uff0capply\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u8fdb\u884c\u81ea\u5b9a\u4e49\u7684\u805a\u5408\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u52a0\u6743\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5206\u7ec4\u6570\u636e\u7684\u6570\u636e\u6846<\/p>\n<p>df_grouped = pd.DataFrame({<\/p>\n<p>    &#39;Group&#39;: [&#39;A&#39;, &#39;A&#39;, &#39;B&#39;, &#39;B&#39;],<\/p>\n<p>    &#39;Value&#39;: [10, 20, 30, 40],<\/p>\n<p>    &#39;Weight&#39;: [1, 2, 1, 2]<\/p>\n<p>})<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570<\/strong><\/h2>\n<p>def weighted_average(group):<\/p>\n<p>    return (group[&#39;Value&#39;] * group[&#39;Weight&#39;]).sum() \/ group[&#39;Weight&#39;].sum()<\/p>\n<h2><strong>\u4f7f\u7528apply\u51fd\u6570\u8fdb\u884c\u5206\u7ec4\u805a\u5408<\/strong><\/h2>\n<p>grouped_result = df_grouped.groupby(&#39;Group&#39;).apply(weighted_average)<\/p>\n<p>print(grouped_result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6848\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0capply\u51fd\u6570\u5728\u6570\u636e\u5206\u6790\u3001\u6e05\u6d17\u3001\u8f6c\u6362\u3001\u7279\u5f81\u5de5\u7a0b\u548c\u805a\u5408\u7b49\u65b9\u9762\u90fd\u6709\u7740\u91cd\u8981\u7684\u5e94\u7528\u4ef7\u503c\u3002\u638c\u63e1apply\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u7684apply\u51fd\u6570\u6709\u4ec0\u4e48\u7528\uff1f<\/strong><br \/>apply\u51fd\u6570\u5728Python\u4e2d\u901a\u5e38\u7528\u4e8e\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u4e8e\u6570\u636e\u7ed3\u6784\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5728Pandas\u5e93\u4e2d\uff0capply\u53ef\u4ee5\u7528\u4e8e\u5bf9DataFrame\u6216Series\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u9010\u884c\u6216\u9010\u5217\u7684\u64cd\u4f5c\uff0c\u4f7f\u5f97\u6570\u636e\u5904\u7406\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Pandas\u7684apply\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u7528\u6cd5\u548c\u6280\u5de7\uff1f<\/strong><br \/>\u5728Pandas\u4e2d\uff0capply\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u591a\u79cd\u573a\u666f\u3002\u5e38\u89c1\u7528\u6cd5\u5305\u62ec\u5bf9DataFrame\u7684\u67d0\u4e00\u5217\u8fdb\u884c\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u5e94\u7528\uff0c\u6216\u5bf9\u6574\u4e2aDataFrame\u8fdb\u884c\u64cd\u4f5c\u3002\u4f7f\u7528lambda\u51fd\u6570\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eaxis\u53c2\u6570\u6765\u9009\u62e9\u662f\u6309\u884c\uff08axis=0\uff09\u8fd8\u662f\u6309\u5217\uff08axis=1\uff09\u5e94\u7528\u51fd\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8apply\u51fd\u6570\u7684\u6267\u884c\u6548\u7387\uff1f<\/strong><br \/>\u867d\u7136apply\u51fd\u6570\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5411\u91cf\u5316\u64cd\u4f5c\u3001\u4f7f\u7528NumPy\u5e93\u7684\u76f8\u5173\u51fd\u6570\uff0c\u6216\u662f\u5229\u7528\u5176\u4ed6\u65b9\u6cd5\u5982list comprehension\u7b49\u3002\u8fd9\u4e9b\u66ff\u4ee3\u65b9\u6848\u901a\u5e38\u80fd\u591f\u663e\u8457\u51cf\u5c11\u8ba1\u7b97\u65f6\u95f4\uff0c\u5e76\u63d0\u5347\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0capply\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u5728Pandas\u5e93\u4e2d\u5bf9\u6570\u636e\u6846\u7684\u884c\u6216\u5217\u5e94\u7528\u7279\u5b9a\u7684\u51fd\u6570\u3002apply\u51fd\u6570\u53ef\u4ee5\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":926543,"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\/926535"}],"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=926535"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/926535\/revisions"}],"predecessor-version":[{"id":926544,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/926535\/revisions\/926544"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/926543"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=926535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=926535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=926535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}