{"id":932163,"date":"2024-12-26T17:53:03","date_gmt":"2024-12-26T09:53:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/932163.html"},"modified":"2024-12-26T17:53:05","modified_gmt":"2024-12-26T09:53:05","slug":"python%e5%a6%82%e4%bd%95%e6%96%b0%e5%bb%ba%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/932163.html","title":{"rendered":"python\u5982\u4f55\u65b0\u5efa\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070132\/5fd84edc-55f0-4d48-b35c-bb96b2e8ad0f.webp\" alt=\"python\u5982\u4f55\u65b0\u5efa\u5217\" \/><\/p>\n<p><p> <strong>\u5728Pandas\u4e2d\uff0cPython\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u51fd\u6570\u3001\u5408\u5e76\u6570\u636e\u6846\u7b49\u65b9\u6cd5\u65b0\u5efa\u5217\uff0c\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002<\/strong>\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528\u51fd\u6570\u53ef\u4ee5\u5728\u521b\u5efa\u65b0\u5217\u65f6\u8fdb\u884c\u6570\u636e\u5904\u7406\uff0c\u5408\u5e76\u6570\u636e\u6846\u5141\u8bb8\u4ece\u5176\u4ed6\u6570\u636e\u6846\u4e2d\u5f15\u5165\u65b0\u5217\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u6570\u636e\u5206\u6790\u7684\u9700\u8981\u7075\u6d3b\u5730\u64cd\u4f5c\u6570\u636e\u6846\u3002\u4ee5\u4e0b\u662f\u5bf9\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\u201c\u76f4\u63a5\u8d4b\u503c\u201d\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a\u76f4\u63a5\u8d4b\u503c\u662f\u521b\u5efa\u65b0\u5217\u7684\u6700\u76f4\u63a5\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5df2\u6709\u5217\u7684\u64cd\u4f5c\u7ed3\u679c\u6216\u5e38\u6570\u503c\u6765\u8d4b\u4e88\u65b0\u5217\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u8fd0\u7b97\u6216\u6807\u8bb0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c\u521b\u5efa\u65b0\u5217<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u7b80\u5355\u3001\u6700\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bf9\u73b0\u6709\u5217\u8fdb\u884c\u64cd\u4f5c\uff0c\u6216\u8005\u76f4\u63a5\u8d4b\u4e88\u5e38\u6570\u503c\u6765\u521b\u5efa\u65b0\u5217\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u76f4\u63a5\u8d4b\u503c\u793a\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u901a\u8fc7\u6570\u636e\u6846\u7684\u5217\u540d\u6765\u521b\u5efa\u548c\u8d4b\u503c\u65b0\u5217\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u73b0\u6709\u5217\u7684\u503c\u6765\u521b\u5efa\u65b0\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\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>\u521b\u5efa\u65b0\u5217&#39;C&#39;\uff0c\u5176\u503c\u4e3a\u5217&#39;A&#39;\u548c\u5217&#39;B&#39;\u4e4b\u548c<\/strong><\/h2>\n<p>df[&#39;C&#39;] = df[&#39;A&#39;] + df[&#39;B&#39;]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6570\u636e\u6846 <code>df<\/code> \u65b0\u589e\u4e86\u4e00\u5217 <code>C<\/code>\uff0c\u5176\u503c\u4e3a\u5217 <code>A<\/code> \u548c\u5217 <code>B<\/code> \u7684\u548c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8d4b\u4e88\u5e38\u6570\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u65b0\u5217\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8d4b\u4e88\u4e00\u4e2a\u5e38\u6570\u503c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4e3a\u6240\u6709\u884c\u8d4b\u4e88\u76f8\u540c\u503c\u7684\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u65b0\u5217&#39;D&#39;\uff0c\u6240\u6709\u884c\u7684\u503c\u90fd\u4e3a10<\/p>\n<p>df[&#39;D&#39;] = 10<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u4e3a <code>df<\/code> \u65b0\u589e\u4e86\u4e00\u5217 <code>D<\/code>\uff0c\u6240\u6709\u884c\u7684\u503c\u90fd\u4e3a10\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u51fd\u6570\u521b\u5efa\u65b0\u5217<\/p>\n<\/p>\n<p><p>Python\u5141\u8bb8\u4f7f\u7528\u51fd\u6570\u6765\u5904\u7406\u6570\u636e\u5e76\u521b\u5efa\u65b0\u5217\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u590d\u6742\u5904\u7406\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Pandas\u63d0\u4f9b\u7684\u5185\u7f6e\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u4e88\u65b0\u5217\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>apply()<\/code> \u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528apply\u51fd\u6570\u521b\u5efa\u65b0\u5217&#39;E&#39;\uff0c\u5176\u503c\u4e3a\u5217&#39;A&#39;\u7684\u5e73\u65b9<\/p>\n<p>df[&#39;E&#39;] = df[&#39;A&#39;].apply(lambda x: x2)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>apply()<\/code> \u51fd\u6570\u7528\u4e8e\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u64cd\u4f5c\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9\u65b0\u5217 <code>E<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u51fd\u6570\u5e76\u5e94\u7528\u4e8e\u6570\u636e\u6846\u7684\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<p>def custom_function(x):<\/p>\n<p>    return x * 2 + 1<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u521b\u5efa\u65b0\u5217&#39;F&#39;<\/strong><\/h2>\n<p>df[&#39;F&#39;] = df[&#39;B&#39;].apply(custom_function)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570 <code>custom_function<\/code> \u5c06\u5217 <code>B<\/code> \u7684\u6bcf\u4e2a\u5143\u7d20\u4e58\u4ee52\u518d\u52a01\uff0c\u7ed3\u679c\u8d4b\u7ed9\u65b0\u5217 <code>F<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u901a\u8fc7\u5408\u5e76\u6570\u636e\u6846\u521b\u5efa\u65b0\u5217<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u5e76\u6570\u636e\u6846\uff0c\u53ef\u4ee5\u5c06\u53e6\u4e00\u4e2a\u6570\u636e\u6846\u7684\u5217\u5f15\u5165\u5230\u5f53\u524d\u6570\u636e\u6846\u4e2d\uff0c\u4ece\u800c\u521b\u5efa\u65b0\u5217\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5408\u5e76\u4e24\u4e2a\u6570\u636e\u6846<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>merge()<\/code> \u51fd\u6570\u53ef\u4ee5\u5408\u5e76\u4e24\u4e2a\u6570\u636e\u6846\uff0c\u8fd9\u5728\u9700\u8981\u5c06\u4e24\u4e2a\u6570\u636e\u96c6\u7684\u4fe1\u606f\u7ed3\u5408\u8d77\u6765\u65f6\u975e\u5e38\u6709\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u53e6\u4e00\u4e2a\u6570\u636e\u6846<\/p>\n<p>df2 = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;G&#39;: [7, 8, 9]<\/p>\n<p>})<\/p>\n<h2><strong>\u5408\u5e76df\u548cdf2\uff0c\u57fa\u4e8e\u5217&#39;A&#39;<\/strong><\/h2>\n<p>merged_df = pd.merge(df, df2, on=&#39;A&#39;)<\/p>\n<p>print(merged_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>merge()<\/code> \u51fd\u6570\u57fa\u4e8e\u5217 <code>A<\/code> \u5c06 <code>df<\/code> \u548c <code>df2<\/code> \u5408\u5e76\uff0c\u65b0\u589e\u4e86\u5217 <code>G<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528concat\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6c34\u5e73\u62fc\u63a5\u6570\u636e\u6846\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>concat()<\/code> \u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u53e6\u4e00\u4e2a\u6570\u636e\u6846<\/p>\n<p>df3 = pd.DataFrame({<\/p>\n<p>    &#39;H&#39;: [10, 11, 12]<\/p>\n<p>})<\/p>\n<h2><strong>\u6c34\u5e73\u62fc\u63a5df\u548cdf3<\/strong><\/h2>\n<p>concatenated_df = pd.concat([df, df3], axis=1)<\/p>\n<p>print(concatenated_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>concat()<\/code> \u51fd\u6570\u5c06 <code>df<\/code> \u548c <code>df3<\/code> \u6c34\u5e73\u62fc\u63a5\uff0c\u65b0\u589e\u4e86\u5217 <code>H<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u57fa\u4e8e\u6761\u4ef6\u521b\u5efa\u65b0\u5217<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u6765\u521b\u5efa\u65b0\u5217\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u548c\u8d4b\u503c\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u6761\u4ef6\u5224\u65ad<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7 <code>numpy.where()<\/code> \u51fd\u6570\u5b9e\u73b0\u6761\u4ef6\u5224\u65ad\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u6839\u636e\u6761\u4ef6\u521b\u5efa\u65b0\u5217&#39;I&#39;\uff0c\u5982\u679c&#39;A&#39;\u5927\u4e8e2\uff0c\u5219\u503c\u4e3a&#39;True&#39;\uff0c\u5426\u5219\u4e3a&#39;False&#39;<\/strong><\/h2>\n<p>df[&#39;I&#39;] = np.where(df[&#39;A&#39;] &gt; 2, True, False)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>np.where()<\/code> \u51fd\u6570\u7528\u4e8e\u5224\u65ad <code>A<\/code> \u5217\u7684\u503c\u662f\u5426\u5927\u4e8e2\uff0c\u5e76\u6839\u636e\u5224\u65ad\u7ed3\u679c\u8d4b\u503c\u7ed9\u65b0\u5217 <code>I<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15<\/strong><\/p>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u76f4\u63a5\u8d4b\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u65b0\u5217&#39;J&#39;\uff0c\u521d\u59cb\u503c\u4e3a0<\/p>\n<p>df[&#39;J&#39;] = 0<\/p>\n<h2><strong>\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u4fee\u6539&#39;J&#39;\u5217\u7684\u503c<\/strong><\/h2>\n<p>df.loc[df[&#39;B&#39;] &gt; 4, &#39;J&#39;] = 1<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e86\u65b0\u5217 <code>J<\/code>\uff0c\u521d\u59cb\u503c\u4e3a0\uff0c\u7136\u540e\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u5c06 <code>B<\/code> \u5217\u5927\u4e8e4\u7684\u884c\u5bf9\u5e94\u7684 <code>J<\/code> \u5217\u7684\u503c\u4fee\u6539\u4e3a1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\u521b\u5efa\u65b0\u5217<\/p>\n<\/p>\n<p><p>Pandas\u4e0e\u5176\u4ed6Python\u5e93\u7684\u7ed3\u5408\u4f7f\u7528\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u6570\u636e\u5904\u7406\u548c\u65b0\u5217\u521b\u5efa\u7684\u80fd\u529b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ed3\u5408NumPy\u8fdb\u884c\u5411\u91cf\u5316\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u5e93\u7684\u5f3a\u5927\u529f\u80fd\u53ef\u4ee5\u7528\u4e8ePandas\u6570\u636e\u6846\u7684\u65b0\u5217\u521b\u5efa\uff0c\u5c24\u5176\u662f\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u65f6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u7684\u5411\u91cf\u5316\u64cd\u4f5c\u521b\u5efa\u65b0\u5217&#39;K&#39;<\/strong><\/h2>\n<p>df[&#39;K&#39;] = np.log(df[&#39;A&#39;] + 1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u4f7f\u7528NumPy\u7684 <code>log()<\/code> \u51fd\u6570\u5bf9 <code>A<\/code> \u5217\u7684\u503c\u8fdb\u884c\u5bf9\u6570\u8fd0\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u7ed9\u65b0\u5217 <code>K<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u5408Scikit-learn\u8fdb\u884c\u6570\u636e\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>Scikit-learn\u662f\u4e00\u6b3e\u5e38\u7528\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u9884\u5904\u7406\u548c\u7279\u5f81\u5de5\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import MinMaxScaler<\/p>\n<h2><strong>\u4f7f\u7528Scikit-learn\u7684MinMaxScaler\u8fdb\u884c\u6570\u636e\u5f52\u4e00\u5316<\/strong><\/h2>\n<p>scaler = MinMaxScaler()<\/p>\n<p>df[&#39;L&#39;] = scaler.fit_transform(df[[&#39;B&#39;]])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cScikit-learn\u7684 <code>MinMaxScaler<\/code> \u7528\u4e8e\u5bf9 <code>B<\/code> \u5217\u7684\u6570\u636e\u8fdb\u884c\u5f52\u4e00\u5316\u5904\u7406\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u7ed9\u65b0\u5217 <code>L<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u4e2d\u5229\u7528Pandas\u5e93\u63d0\u4f9b\u7684\u591a\u79cd\u65b9\u6cd5\u80fd\u591f\u7075\u6d3b\u5730\u521b\u5efa\u65b0\u5217\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u6570\u636e\u60c5\u51b5\uff0c\u53ef\u4ee5\u9009\u62e9\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u51fd\u6570\u3001\u5408\u5e76\u6570\u636e\u6846\u3001\u57fa\u4e8e\u6761\u4ef6\u5224\u65ad\u4ee5\u53ca\u7ed3\u5408\u5176\u4ed6\u5e93\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u6570\u636e\u5206\u6790\u3001\u6570\u636e\u5904\u7406\u548c\u7279\u5f81\u5de5\u7a0b\u4e2d\u90fd\u975e\u5e38\u5b9e\u7528\uff0c\u662f\u63d0\u5347\u6570\u636e\u5206\u6790\u6548\u7387\u548c\u51c6\u786e\u5ea6\u7684\u91cd\u8981\u624b\u6bb5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7684Pandas\u4e2d\u521b\u5efa\u65b0\u5217\uff1f<\/strong><br \/>\u5728Pandas\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\u521b\u5efa\u65b0\u5217\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528DataFrame\u7684\u7d22\u5f15\u6765\u6307\u5b9a\u65b0\u5217\u7684\u540d\u79f0\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u4e3a\u4e00\u4e2a\u5217\u8868\u3001Series\u6216\u901a\u8fc7\u8fd0\u7b97\u751f\u6210\u7684\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}\ndf = pd.DataFrame(data)\ndf[&#39;C&#39;] = df[&#39;A&#39;] + df[&#39;B&#39;]  # \u521b\u5efa\u65b0\u5217C\uff0c\u503c\u4e3aA\u5217\u4e0eB\u5217\u7684\u548c\nprint(df)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u521b\u5efa\u65b0\u5217\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\u521b\u5efa\u65b0\u5217\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\uff1a<\/p>\n<ol>\n<li><strong>\u8d4b\u503c\u6cd5<\/strong>\uff1a\u76f4\u63a5\u5c06\u4e00\u4e2a\u5217\u8868\u6216Series\u8d4b\u7ed9\u65b0\u5217\u3002<\/li>\n<li><strong>\u4f7f\u7528apply\u51fd\u6570<\/strong>\uff1a\u901a\u8fc7apply\u51fd\u6570\u5bf9\u73b0\u6709\u5217\u8fdb\u884c\u64cd\u4f5c\u751f\u6210\u65b0\u5217\u3002<\/li>\n<li><strong>\u6761\u4ef6\u7b5b\u9009<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u521b\u5efa\u65b0\u5217\uff0c\u4f8b\u5982\u4f7f\u7528np.where()\u51fd\u6570\u6765\u751f\u6210\u57fa\u4e8e\u6761\u4ef6\u7684\u5217\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5408\u5e76<\/strong>\uff1a\u901a\u8fc7merge()\u65b9\u6cd5\u5c06\u4e24\u4e2aDataFrame\u5408\u5e76\uff0c\u4ece\u800c\u6dfb\u52a0\u65b0\u7684\u5217\u3002<\/li>\n<\/ol>\n<p><strong>\u5982\u4f55\u5229\u7528Python\u7684NumPy\u5e93\u4e3aDataFrame\u6dfb\u52a0\u65b0\u5217\uff1f<\/strong><br \/>NumPy\u5e93\u53ef\u4ee5\u7528\u4e8e\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\uff0c\u7ed3\u5408Pandas\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u7b80\u5316\u65b0\u5217\u7684\u521b\u5efa\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7NumPy\u7684\u64cd\u4f5c\u751f\u6210\u65b0\u5217\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\nimport numpy as np\n\ndata = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}\ndf = pd.DataFrame(data)\ndf[&#39;C&#39;] = np.sqrt(df[&#39;A&#39;] * df[&#39;B&#39;])  # \u521b\u5efa\u65b0\u5217C\uff0c\u503c\u4e3aA\u5217\u4e0eB\u5217\u4e58\u79ef\u7684\u5e73\u65b9\u6839\nprint(df)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u6570\u5b66\u8fd0\u7b97\u65f6\uff0c\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Pandas\u4e2d\uff0cPython\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u51fd\u6570\u3001\u5408\u5e76\u6570\u636e\u6846\u7b49\u65b9\u6cd5\u65b0\u5efa\u5217\uff0c\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002\u76f4\u63a5\u8d4b\u503c\u662f [&hellip;]","protected":false},"author":3,"featured_media":932166,"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\/932163"}],"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=932163"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932163\/revisions"}],"predecessor-version":[{"id":932167,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932163\/revisions\/932167"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/932166"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=932163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=932163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=932163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}