{"id":1142981,"date":"2025-01-08T22:45:49","date_gmt":"2025-01-08T14:45:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1142981.html"},"modified":"2025-01-08T22:45:52","modified_gmt":"2025-01-08T14:45:52","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e4%b8%80%e5%88%97%e5%a1%ab%e6%bb%a1%e7%9b%b8%e5%90%8c%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1142981.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u4e00\u5217\u586b\u6ee1\u76f8\u540c\u7684\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180827\/7a91ad07-f25e-4820-86f8-507fa53b0e4d.webp\" alt=\"python\u5982\u4f55\u8ba9\u4e00\u5217\u586b\u6ee1\u76f8\u540c\u7684\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5c06\u4e00\u5217\u586b\u6ee1\u76f8\u540c\u7684\u5b57\u7b26\u4e32<\/strong>\uff0c<strong>\u4f8b\u5982\u4f7f\u7528Pandas\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001Numpy\u5e93\u7b49\u3002<\/strong> <strong>\u5176\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<\/strong> <strong>\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5de5\u5177\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u9886\u57df\u3002\u8981\u4f7f\u7528Pandas\u586b\u5145\u4e00\u5217\u76f8\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u901a\u5e38\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165Pandas\u5e93<\/strong>\uff1a\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165Pandas\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5Pandas\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install pandas<\/code>\u6765\u5b89\u88c5\u3002<\/li>\n<li><strong>\u521b\u5efaDataFrame<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u6216\u8005\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u521b\u5efa\u4e00\u4e2aDataFrame\u3002<\/li>\n<li><strong>\u586b\u5145\u6570\u636e<\/strong>\uff1a\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\uff0c\u5c06\u76ee\u6807\u5217\u586b\u6ee1\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/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 = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u586b\u5145&#39;B&#39;\u5217\u4e3a\u76f8\u540c\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>df[&#39;B&#39;] = &#39;filled_string&#39;<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e24\u5217\u7684DataFrame\uff0c\u7136\u540e\u5c06&#39;B&#39;\u5217\u7684\u6240\u6709\u503c\u66ff\u6362\u4e3a\u5b57\u7b26\u4e32&#39;filled_string&#39;\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u5feb\u901f\u5730\u751f\u6210\u548c\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u4e00\u4e2a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u76ee\u6807\u5217\u957f\u5ea6\u7684\u7a7a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">n = 10  # \u5047\u8bbe\u76ee\u6807\u5217\u7684\u957f\u5ea6\u4e3a10<\/p>\n<p>filled_list = [&#39;filled_string&#39; for _ in range(n)]<\/p>\n<p>print(filled_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u5c06\u751f\u6210\u4e00\u4e2a\u957f\u5ea6\u4e3a10\u7684\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u5b57\u7b26\u4e32&#39;filled_string&#39;\u3002<\/p>\n<\/p>\n<p><h4>2. \u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u5e93\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\u7684\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;Filled_Column&#39;: filled_list})<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u4f60\u5c31\u5f97\u5230\u4e86\u4e00\u4e2aDataFrame\uff0c\u5176\u4e2d&#39;Filled_Column&#39;\u5217\u586b\u6ee1\u4e86\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Numpy\u5e93<\/h3>\n<\/p>\n<p><p>Numpy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684N\u7ef4\u6570\u7ec4\u5bf9\u8c61\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u586b\u5145\u4e00\u5217\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165Numpy\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165Numpy\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5Numpy\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install numpy<\/code>\u6765\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u4e00\u4e2aNumpy\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Numpy\u7684<code>full<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u586b\u6ee1\u76f8\u540c\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">n = 10  # \u5047\u8bbe\u76ee\u6807\u5217\u7684\u957f\u5ea6\u4e3a10<\/p>\n<p>filled_array = np.full(n, &#39;filled_string&#39;)<\/p>\n<p>print(filled_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5c06Numpy\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u5c06Numpy\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\u7684\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;Filled_Column&#39;: filled_array})<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b80\u5355\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528\u4efb\u4f55\u5916\u90e8\u5e93\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u5faa\u73af\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filled_list = []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5faa\u73af\u586b\u5145\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6765\u586b\u5145\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">n = 10  # \u5047\u8bbe\u76ee\u6807\u5217\u7684\u957f\u5ea6\u4e3a10<\/p>\n<p>for _ in range(n):<\/p>\n<p>    filled_list.append(&#39;filled_string&#39;)<\/p>\n<p>print(filled_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame<\/h4>\n<\/p>\n<p><p>\u6700\u7ec8\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\u7684\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;Filled_Column&#39;: filled_list})<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Python\u5185\u7f6e\u7684<code>map<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>map<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u586b\u6ee1\u76f8\u540c\u5b57\u7b26\u4e32\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>map<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u586b\u6ee1\u76f8\u540c\u5b57\u7b26\u4e32\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">n = 10  # \u5047\u8bbe\u76ee\u6807\u5217\u7684\u957f\u5ea6\u4e3a10<\/p>\n<p>filled_list = list(map(lambda _: &#39;filled_string&#39;, range(n)))<\/p>\n<p>print(filled_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u4f7f\u7528Pandas\u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\u7684\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;Filled_Column&#39;: filled_list})<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u5217\u586b\u6ee1\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u3002<strong>\u4f7f\u7528Pandas\u5e93\u662f\u6700\u5e38\u7528\u548c\u6700\u65b9\u4fbf\u7684\u65b9\u6cd5<\/strong>\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u66f4\u559c\u6b22\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001Numpy\u5e93\u3001\u7b80\u5355\u5faa\u73af\u6216\u8005<code>map<\/code>\u51fd\u6570\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u504f\u597d\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u5feb\u901f\u3001\u6709\u6548\u5730\u5b9e\u73b0\u76ee\u6807\u3002\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u5c06\u5927\u5927\u63d0\u9ad8\u4f60\u7684\u5de5\u4f5c\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e3aDataFrame\u7684\u4e00\u5217\u586b\u5145\u76f8\u540c\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Pandas\u5e93\u65f6\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4e3aDataFrame\u7684\u7279\u5b9a\u5217\u586b\u5145\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u3002\u53ea\u9700\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u4f8b\u5982<code>df[&#39;\u5217\u540d&#39;] = &#39;\u5b57\u7b26\u4e32&#39;<\/code>\uff0c\u5373\u53ef\u5c06\u6574\u5217\u586b\u5145\u4e3a\u6240\u9700\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>\u5728Pandas\u4e2d\uff0c\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u5e76\u586b\u5145\u76f8\u540c\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u8981\u5728\u73b0\u6709\u7684DataFrame\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5217\uff0c\u5e76\u5c06\u5176\u586b\u5145\u4e3a\u76f8\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u4e8e<code>df[&#39;\u65b0\u5217\u540d&#39;] = &#39;\u5b57\u7b26\u4e32&#39;<\/code>\u7684\u8bed\u6cd5\u3002\u8fd9\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u6570\u636e\u96c6\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u4fe1\u606f\uff0c\u8fd8\u80fd\u4fdd\u6301\u6570\u636e\u7ed3\u6784\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u7684\u5217\u8868\u4e2d\u586b\u5145\u76f8\u540c\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u586b\u5145\u76f8\u540c\u5b57\u7b26\u4e32\u7684\u5217\u8868\uff0c\u4f8b\u5982<code>my_list = [&#39;\u5b57\u7b26\u4e32&#39;] * n<\/code>\uff0c\u5176\u4e2d<code>n<\/code>\u662f\u4f60\u5e0c\u671b\u586b\u5145\u7684\u5143\u7d20\u6570\u91cf\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u9700\u8981\u5feb\u901f\u751f\u6210\u56fa\u5b9a\u6a21\u5f0f\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5c06\u4e00\u5217\u586b\u6ee1\u76f8\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\u4f7f\u7528Pandas\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001Numpy\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1142988,"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\/1142981"}],"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=1142981"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142981\/revisions"}],"predecessor-version":[{"id":1142990,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142981\/revisions\/1142990"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1142988"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1142981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1142981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1142981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}