{"id":1141634,"date":"2025-01-08T22:32:50","date_gmt":"2025-01-08T14:32:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141634.html"},"modified":"2025-01-08T22:32:52","modified_gmt":"2025-01-08T14:32:52","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%80%e5%88%97%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%97%a5%e6%9c%9f%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141634.html","title":{"rendered":"python\u5982\u4f55\u5c06\u4e00\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104323\/a1b997c9-c007-442e-ab1a-2b47b9217095.webp\" alt=\"python\u5982\u4f55\u5c06\u4e00\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u5c06\u4e00\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c<strong>\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528datetime\u6a21\u5757\u3001\u5904\u7406\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f<\/strong>\u3002\u672c\u6587\u5c06\u91cd\u70b9\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528pandas\u5e93\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff0c\u5e76\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5904\u7406\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u5904\u7406\u5e93\u4e4b\u4e00\uff0c\u5177\u6709\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\u3002\u4f7f\u7528pandas\u5c06\u4e00\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u8fd8\u6ca1\u6709\u5b89\u88c5pandas\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bfc\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u9700\u8981\u5904\u7406\u7684\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2aCSV\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u5217\u9700\u8981\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u7684\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>pandas.read_csv<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5bfc\u5165\u5230DataFrame\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528pd.to_datetime\u51fd\u6570<\/h4>\n<\/p>\n<p><p>pandas\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u51fd\u6570<code>pd.to_datetime<\/code>\uff0c\u53ef\u4ee5\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f<\/p>\n<p>df[&#39;date_column&#39;] = pd.to_datetime(df[&#39;date_column&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5904\u7406\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u65e5\u671f\u683c\u5f0f\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002<code>pd.to_datetime<\/code>\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u5927\u591a\u6570\u5e38\u89c1\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u4f46\u6709\u65f6\u9700\u8981\u624b\u52a8\u6307\u5b9a\u65e5\u671f\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u624b\u52a8\u6307\u5b9a\u65e5\u671f\u683c\u5f0f<\/p>\n<p>df[&#39;date_column&#39;] = pd.to_datetime(df[&#39;date_column&#39;], format=&#39;%Y-%m-%d&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528datetime\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9664\u4e86pandas\u5e93\u5916\uff0cPython\u5185\u7f6e\u7684datetime\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u867d\u7136\u4e0d\u5982pandas\u65b9\u4fbf\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165datetime\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8f6c\u6362\u65e5\u671f\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u9700\u8981\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime.strptime<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_list = [&#39;2023-01-01&#39;, &#39;2023-01-02&#39;, &#39;2023-01-03&#39;]<\/p>\n<p>date_format = &#39;%Y-%m-%d&#39;<\/p>\n<h2><strong>\u8f6c\u6362\u65e5\u671f\u683c\u5f0f<\/strong><\/h2>\n<p>converted_dates = [datetime.strptime(date, date_format) for date in date_list]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u65e5\u671f\u683c\u5f0f\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u6b63\u786e\u8bc6\u522b\u548c\u5904\u7406\u8fd9\u4e9b\u4e0d\u540c\u7684\u683c\u5f0f\u662f\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5e38\u89c1\u65e5\u671f\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5e38\u89c1\u7684\u65e5\u671f\u683c\u5f0f\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5e74-\u6708-\u65e5\uff08\u59822023-01-01\uff09<\/li>\n<li>\u6708\/\u65e5\/\u5e74\uff08\u598201\/01\/2023\uff09<\/li>\n<li>\u65e5-\u6708-\u5e74\uff08\u598201-01-2023\uff09<\/li>\n<\/ul>\n<p><h4>2. \u4f7f\u7528pd.to_datetime\u81ea\u52a8\u8bc6\u522b<\/h4>\n<\/p>\n<p><p>pandas\u7684<code>pd.to_datetime<\/code>\u51fd\u6570\u53ef\u4ee5\u81ea\u52a8\u8bc6\u522b\u591a\u79cd\u5e38\u89c1\u7684\u65e5\u671f\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u81ea\u52a8\u8bc6\u522b\u65e5\u671f\u683c\u5f0f<\/p>\n<p>df[&#39;date_column&#39;] = pd.to_datetime(df[&#39;date_column&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u624b\u52a8\u6307\u5b9a\u65e5\u671f\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u7279\u6b8a\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u53ef\u4ee5\u624b\u52a8\u6307\u5b9a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u624b\u52a8\u6307\u5b9a\u65e5\u671f\u683c\u5f0f<\/p>\n<p>df[&#39;date_column&#39;] = pd.to_datetime(df[&#39;date_column&#39;], format=&#39;%d-%m-%Y&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u7f3a\u5931\u503c\u548c\u9519\u8bef\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u4e2d\u53ef\u80fd\u5305\u542b\u7f3a\u5931\u503c\u6216\u9519\u8bef\u6570\u636e\u3002\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u662f\u786e\u4fdd\u6570\u636e\u8d28\u91cf\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5904\u7406\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pandas\u7684<code>dropna<\/code>\u65b9\u6cd5\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\uff0c\u6216\u8005\u4f7f\u7528<code>fillna<\/code>\u65b9\u6cd5\u586b\u5145\u7f3a\u5931\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/p>\n<p>df.dropna(subset=[&#39;date_column&#39;], inplace=True)<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df[&#39;date_column&#39;].fillna(pd.to_datetime(&#39;1900-01-01&#39;), inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5904\u7406\u9519\u8bef\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u683c\u5f0f\u9519\u8bef\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>errors<\/code>\u53c2\u6570\u6765\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5ffd\u7565\u9519\u8bef\u6570\u636e<\/p>\n<p>df[&#39;date_column&#39;] = pd.to_datetime(df[&#39;date_column&#39;], errors=&#39;coerce&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\uff0c\u4e3b\u8981\u4f7f\u7528\u4e86pandas\u5e93\u548cdatetime\u6a21\u5757\u3002<strong>pandas\u5e93\u5177\u6709\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u63a8\u8350\u4f7f\u7528<\/strong>\u3002\u5728\u5904\u7406\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pd.to_datetime<\/code>\u51fd\u6570\u81ea\u52a8\u8bc6\u522b\uff0c\u6216\u8005\u624b\u52a8\u6307\u5b9a\u65e5\u671f\u683c\u5f0f\u3002\u5904\u7406\u7f3a\u5931\u503c\u548c\u9519\u8bef\u6570\u636e\u4e5f\u662f\u786e\u4fdd\u6570\u636e\u8d28\u91cf\u7684\u5173\u952e\u6b65\u9aa4\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5904\u7406\u65e5\u671f\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u5e76\u8f6c\u6362\u5b57\u7b26\u4e32\u4e3a\u65e5\u671f\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>to_datetime<\/code>\u51fd\u6570\u6765\u8bc6\u522b\u548c\u8f6c\u6362\u5b57\u7b26\u4e32\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u81ea\u52a8\u89e3\u6790\u5e38\u89c1\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u3002\u5982\u679c\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u660e\u786e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a<code>format<\/code>\u53c2\u6570\u6765\u544a\u8bc9<code>to_datetime<\/code>\u5982\u4f55\u89e3\u6790\u65e5\u671f\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u79cd\u5e93\u6bd4\u8f83\u9002\u5408\u8fdb\u884c\u65e5\u671f\u683c\u5f0f\u8f6c\u6362\uff1f<\/strong><br \/><code>pandas<\/code>\u662f\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u548c\u65e5\u671f\u683c\u5f0f\u8f6c\u6362\u7684\u5f3a\u5927\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65e5\u671f\u65f6\u95f4\u5904\u7406\u529f\u80fd\uff0c\u80fd\u591f\u5feb\u901f\u9ad8\u6548\u5730\u5bf9\u65e5\u671f\u8fdb\u884c\u64cd\u4f5c\u3002\u6b64\u5916\uff0c<code>datetime<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u4e8e\u65e5\u671f\u7684\u57fa\u672c\u5904\u7406\uff0c\u4f46\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c<code>pandas<\/code>\u66f4\u4e3a\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u65e5\u671f\u8f6c\u6362\u65f6\uff0c\u5982\u4f55\u5904\u7406\u65e0\u6548\u65e5\u671f\u6216\u7a7a\u503c\uff1f<\/strong><br \/>\u4f7f\u7528<code>pandas<\/code>\u7684<code>to_datetime<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>errors<\/code>\u53c2\u6570\u6765\u5904\u7406\u65e0\u6548\u65e5\u671f\u548c\u7a7a\u503c\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e<code>errors=&#39;coerce&#39;<\/code>\u4f1a\u5c06\u65e0\u6cd5\u89e3\u6790\u7684\u65e5\u671f\u8f6c\u6362\u4e3a<code>NaT<\/code>\uff08Not a Time\uff09\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u4e2d\u907f\u514d\u9519\u8bef\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>fillna<\/code>\u65b9\u6cd5\u586b\u5145\u8fd9\u4e9b\u7a7a\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5c06\u4e00\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f \u5728Python\u4e2d\uff0c\u5c06\u4e00\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u4f7f\u7528p [&hellip;]","protected":false},"author":3,"featured_media":1141641,"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\/1141634"}],"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=1141634"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141634\/revisions"}],"predecessor-version":[{"id":1141645,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141634\/revisions\/1141645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141641"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}