{"id":1131054,"date":"2025-01-08T20:45:25","date_gmt":"2025-01-08T12:45:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1131054.html"},"modified":"2025-01-08T20:45:27","modified_gmt":"2025-01-08T12:45:27","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e4%b8%80%e5%88%97%e6%95%b0%e5%90%91%e4%b8%8a%e5%8f%96%e6%95%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1131054.html","title":{"rendered":"Python\u5982\u4f55\u5bf9\u4e00\u5217\u6570\u5411\u4e0a\u53d6\u6574"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100932\/d72d6148-bec8-4a43-855e-119a433dcd8f.webp\" alt=\"Python\u5982\u4f55\u5bf9\u4e00\u5217\u6570\u5411\u4e0a\u53d6\u6574\" \/><\/p>\n<p><p> <strong>Python\u5bf9\u4e00\u5217\u6570\u8fdb\u884c\u5411\u4e0a\u53d6\u6574\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528math\u6a21\u5757\u3001NumPy\u5e93\u4ee5\u53caPandas\u5e93\u7b49\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u5f62\u5f0f\u548c\u9700\u6c42\u3002<\/strong> \u5176\u4e2d\uff0c<strong>math\u6a21\u5757\u9002\u7528\u4e8e\u5355\u4e2a\u6570\u7684\u5904\u7406<\/strong>\uff0c<strong>NumPy\u5e93\u9002\u5408\u5927\u89c4\u6a21\u6570\u7ec4\u7684\u5904\u7406<\/strong>\uff0c\u800c<strong>Pandas\u5e93\u5219\u5728\u6570\u636e\u6846\u64cd\u4f5c\u4e2d\u8868\u73b0\u51fa\u8272<\/strong>\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528math\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93<code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u57fa\u7840\u7684\u6570\u5b66\u51fd\u6570\uff0c<code>math.ceil()<\/code>\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\uff0c\u5b83\u7528\u4e8e\u5411\u4e0a\u53d6\u6574\u5355\u4e2a\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528math.ceil()\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>numbers = [1.2, 2.3, 3.4, 4.5, 5.6]<\/p>\n<p>rounded_numbers = [math.ceil(num) for num in numbers]<\/p>\n<p>print(rounded_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>math.ceil()<\/code>\u51fd\u6570\u9010\u4e2a\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u8fdb\u884c\u5411\u4e0a\u53d6\u6574\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e<\/strong>\uff0c\u4f46\u5982\u679c\u6570\u636e\u91cf\u5f88\u5927\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5904\u7406\u5355\u4e2a\u6570\u503c<\/li>\n<li>\u76f4\u63a5\u96c6\u6210\u5728Python\u6807\u51c6\u5e93\u4e2d\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e<\/li>\n<li>\u65e0\u6cd5\u76f4\u63a5\u64cd\u4f5c\u6570\u636e\u6846<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u5927\u89c4\u6a21\u6570\u7ec4\u548c\u77e9\u9635\u7684\u652f\u6301\uff0c\u5e76\u914d\u6709\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u3002<code>numpy.ceil()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5411\u4e0a\u53d6\u6574\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528numpy.ceil()\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.array([1.2, 2.3, 3.4, 4.5, 5.6])<\/p>\n<p>rounded_numbers = np.ceil(numbers)<\/p>\n<p>print(rounded_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>numpy.ceil()<\/code>\u51fd\u6570\u76f4\u63a5\u5bf9NumPy\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002<strong>NumPy\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u6548\u7387\u975e\u5e38\u9ad8<\/strong>\uff0c\u9002\u5408\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u5b66\u8fd0\u7b97\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9ad8\u6548\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4<\/li>\n<li>\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u5e93<\/li>\n<li>\u5e7f\u6cdb\u5e94\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u989d\u5916\u5b89\u88c5NumPy\u5e93<\/li>\n<li>\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u53ef\u80fd\u6709\u4e00\u5b9a\u7684\u5b66\u4e60\u66f2\u7ebf<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u5904\u7406\u5e93\uff0c<code>pandas.Series<\/code>\u548c<code>pandas.DataFrame<\/code>\u5bf9\u8c61\u662f\u5176\u6838\u5fc3\u3002<code>pandas.Series.apply()<\/code>\u548c<code>pandas.DataFrame.applymap()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528pandas.Series.apply()\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>numbers = pd.Series([1.2, 2.3, 3.4, 4.5, 5.6])<\/p>\n<p>rounded_numbers = numbers.apply(np.ceil)<\/p>\n<p>print(rounded_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>pandas.Series.apply()<\/code>\u65b9\u6cd5\u5bf9Series\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528<code>numpy.ceil()<\/code>\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684Series\u3002<strong>Pandas\u5e93\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u65b9\u9762\u975e\u5e38\u5f3a\u5927<\/strong>\uff0c\u9002\u5408\u9700\u8981\u5bf9\u6570\u636e\u6846\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528pandas.DataFrame.applymap()\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;A&#39;: [1.2, 2.3, 3.4], &#39;B&#39;: [4.5, 5.6, 6.7]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>rounded_df = df.applymap(np.ceil)<\/p>\n<p>print(rounded_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>pandas.DataFrame.applymap()<\/code>\u65b9\u6cd5\u5bf9DataFrame\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528<code>numpy.ceil()<\/code>\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684DataFrame\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6574\u4e2a\u6570\u636e\u6846\u8fdb\u884c\u9010\u5143\u7d20\u64cd\u4f5c\u7684\u573a\u666f<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u529f\u80fd<\/li>\n<li>\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u6846\u64cd\u4f5c<\/li>\n<li>\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><\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u989d\u5916\u5b89\u88c5Pandas\u5e93<\/li>\n<li>\u5bf9\u4e8e\u7b80\u5355\u7684\u5355\u5217\u6570\u636e\u5904\u7406\uff0c\u53ef\u80fd\u663e\u5f97\u8fc7\u4e8e\u590d\u6742<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5bf9\u4e00\u5217\u6570\u8fdb\u884c\u5411\u4e0a\u53d6\u6574\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>math\u6a21\u5757<\/strong>\uff1a\u9002\u7528\u4e8e\u5355\u4e2a\u6570\u503c\u6216\u5c0f\u89c4\u6a21\u6570\u636e\u7684\u5904\u7406\uff0c\u7b80\u5355\u6613\u7528\u3002<\/li>\n<li><strong>NumPy\u5e93<\/strong>\uff1a\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\uff0c\u6548\u7387\u9ad8\uff0c\u529f\u80fd\u5f3a\u5927\u3002<\/li>\n<li><strong>Pandas\u5e93<\/strong>\uff1a\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u6846\u64cd\u4f5c\uff0c\u529f\u80fd\u5168\u9762\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5bf9\u4e00\u5217\u6570\u8fdb\u884c\u5411\u4e0a\u53d6\u6574\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5355\u5217\u6570\u636e\u5904\u7406\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u6846\u64cd\u4f5c\uff0c\u90fd\u80fd\u627e\u5230\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u4e00\u5217\u6570\u8fdb\u884c\u5411\u4e0a\u53d6\u6574\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u7684<code>ceil()<\/code>\u51fd\u6570\u6765\u5bf9\u4e00\u5217\u6570\u8fdb\u884c\u5411\u4e0a\u53d6\u6574\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86<code>numpy<\/code>\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>pip install numpy<\/code>\u6765\u5b89\u88c5\u5b83\u3002\u7136\u540e\uff0c\u5bfc\u5165<code>numpy<\/code>\u5e76\u4f7f\u7528<code>numpy.ceil()<\/code>\u51fd\u6570\u6765\u5904\u7406\u6570\u636e\u3002\u4f8b\u5982\uff0c<code>numpy.ceil([1.1, 2.3, 3.7])<\/code>\u5c06\u8fd4\u56de<code>array([2., 3., 4.])<\/code>\uff0c\u8868\u793a\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u4e86\u5411\u4e0a\u53d6\u6574\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5411\u4e0a\u53d6\u6574\uff1f<\/strong><br \/>\u9664\u4e86<code>numpy<\/code>\u5e93\u5916\uff0cPython\u6807\u51c6\u5e93\u4e2d\u7684<code>math<\/code>\u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86<code>math.ceil()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u5355\u4e2a\u6570\u5b57\u8fdb\u884c\u5411\u4e0a\u53d6\u6574\u3002\u5982\u679c\u4f60\u9700\u8981\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5411\u4e0a\u53d6\u6574\uff0c\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<code>[math.ceil(x) for x in my_list]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u539f\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5411\u4e0a\u53d6\u6574\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u5411\u4e0a\u53d6\u6574\u4f1a\u5f71\u54cd\u6027\u80fd\u5417\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u4f7f\u7528<code>numpy<\/code>\u7684\u5411\u4e0a\u53d6\u6574\u65b9\u6cd5\u901a\u5e38\u66f4\u5177\u6027\u80fd\u4f18\u52bf\uff0c\u56e0\u4e3a<code>numpy<\/code>\u662f\u4e3a\u5904\u7406\u5927\u578b\u6570\u7ec4\u800c\u4f18\u5316\u7684\u3002\u5bf9\u4e8e\u5c0f\u578b\u6570\u636e\u96c6\uff0c\u4f7f\u7528<code>math.ceil()<\/code>\u4e5f\u53ef\u4ee5\u6ee1\u8db3\u9700\u6c42\uff0c\u4f46\u5728\u5904\u7406\u6570\u5343\u6216\u66f4\u591a\u5143\u7d20\u65f6\uff0c<code>numpy<\/code>\u7684\u6570\u7ec4\u64cd\u4f5c\u4f1a\u66f4\u5feb\u4e14\u6548\u7387\u66f4\u9ad8\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bf9\u4e00\u5217\u6570\u8fdb\u884c\u5411\u4e0a\u53d6\u6574\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528math\u6a21\u5757\u3001NumPy\u5e93\u4ee5\u53caPandas\u5e93\u7b49\uff0c\u8fd9\u4e9b\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1131062,"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\/1131054"}],"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=1131054"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131054\/revisions"}],"predecessor-version":[{"id":1131066,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131054\/revisions\/1131066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1131062"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1131054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1131054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1131054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}