{"id":1109700,"date":"2025-01-08T17:12:31","date_gmt":"2025-01-08T09:12:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109700.html"},"modified":"2025-01-08T17:12:34","modified_gmt":"2025-01-08T09:12:34","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%9c%8b%e6%9c%89%e6%b2%a1%e6%9c%89%e7%bc%ba%e5%a4%b1%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1109700.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u770b\u6709\u6ca1\u6709\u7f3a\u5931\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072849\/e5817fa4-6477-48e1-a925-a9eab546b610.webp\" alt=\"python\u4e2d\u5982\u4f55\u770b\u6709\u6ca1\u6709\u7f3a\u5931\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u68c0\u67e5\u662f\u5426\u5b58\u5728\u7f3a\u5931\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>isnull()<\/code>\u51fd\u6570\u3001<code>notnull()<\/code>\u51fd\u6570\u3001\u7ed3\u5408<code>sum()<\/code>\u51fd\u6570\u67e5\u770b\u6bcf\u5217\u7684\u7f3a\u5931\u503c\u603b\u6570\u3001\u4f7f\u7528<code>info()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6570\u636e\u6458\u8981\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>isnull()<\/code>\u51fd\u6570\u7ed3\u5408<code>sum()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u8fd9\u6837\u53ef\u4ee5\u5feb\u901f\u67e5\u770b\u6bcf\u5217\u4e2d\u7f3a\u5931\u503c\u7684\u6570\u91cf\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>isnull()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>isnull()<\/code>\u51fd\u6570\u662fPandas\u5e93\u4e2d\u7528\u4e8e\u68c0\u6d4b\u7f3a\u5931\u503c\u7684\u51fd\u6570\u3002\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u4e0e\u539f\u6570\u636e\u96c6\u5f62\u72b6\u76f8\u540c\u7684DataFrame\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u5e03\u5c14\u503c\uff0c\u6307\u793a\u8be5\u4f4d\u7f6e\u662f\u5426\u4e3a\u7f3a\u5931\u503c\u3002<\/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 = {&#39;A&#39;: [1, 2, None, 4], &#39;B&#39;: [None, 2, 3, 4], &#39;C&#39;: [1, 2, 3, 4]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u68c0\u67e5\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>missing_values = df.isnull()<\/p>\n<p>print(missing_values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u5c06\u8f93\u51fa\u4e00\u4e2a\u5e03\u5c14\u503cDataFrame\uff0c\u5176\u4e2dTrue\u8868\u793a\u7f3a\u5931\u503c\uff0cFalse\u8868\u793a\u975e\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u7ed3\u5408<code>sum()<\/code>\u51fd\u6570\u67e5\u770b\u6bcf\u5217\u7684\u7f3a\u5931\u503c\u603b\u6570<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u6e05\u6670\u5730\u67e5\u770b\u6bcf\u5217\u4e2d\u7f3a\u5931\u503c\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u5c06<code>isnull()<\/code>\u51fd\u6570\u7684\u7ed3\u679c\u4e0e<code>sum()<\/code>\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u6bcf\u5217\u7684\u7f3a\u5931\u503c\u603b\u6570<\/p>\n<p>missing_values_count = df.isnull().sum()<\/p>\n<p>print(missing_values_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u4e00\u4e2aSeries\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u5217\u7684\u7f3a\u5931\u503c\u603b\u6570\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u4f7f\u7528<code>info()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>info()<\/code>\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u67e5\u770b\u6570\u636e\u96c6\u7684\u6458\u8981\u4fe1\u606f\uff0c\u5305\u62ec\u6bcf\u5217\u7684\u6570\u636e\u7c7b\u578b\u548c\u975e\u7f3a\u5931\u503c\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u6570\u636e\u6458\u8981\u4fe1\u606f<\/p>\n<p>df.info()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u867d\u7136\u4e0d\u80fd\u76f4\u63a5\u663e\u793a\u7f3a\u5931\u503c\u7684\u6570\u91cf\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u6bcf\u5217\u975e\u7f3a\u5931\u503c\u7684\u6570\u91cf\uff0c\u95f4\u63a5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u7f3a\u5931\u503c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u4f7f\u7528<code>notnull()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>notnull()<\/code>\u51fd\u6570\u4e0e<code>isnull()<\/code>\u51fd\u6570\u76f8\u53cd\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503cDataFrame\uff0c\u5176\u4e2dTrue\u8868\u793a\u975e\u7f3a\u5931\u503c\uff0cFalse\u8868\u793a\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u975e\u7f3a\u5931\u503c<\/p>\n<p>non_missing_values = df.notnull()<\/p>\n<p>print(non_missing_values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u80fd\u76f4\u63a5\u7528\u6765\u7edf\u8ba1\u7f3a\u5931\u503c\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5b83\u4e5f\u5f88\u6709\u7528\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e94\u3001\u7ed3\u5408<code>sum()<\/code>\u51fd\u6570\u67e5\u770b\u7f3a\u5931\u503c\u5728\u6574\u4e2aDataFrame\u4e2d\u7684\u603b\u6570<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u4e0d\u4ec5\u4ec5\u5173\u5fc3\u6bcf\u5217\u7684\u7f3a\u5931\u503c\u6570\u91cf\uff0c\u8fd8\u9700\u8981\u77e5\u9053\u6574\u4e2aDataFrame\u4e2d\u603b\u5171\u6709\u591a\u5c11\u7f3a\u5931\u503c\u3002\u8fd9\u65f6\u53ef\u4ee5\u5bf9<code>isnull()<\/code>\u7684\u7ed3\u679c\u4f7f\u7528<code>sum().sum()<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u6574\u4e2aDataFrame\u4e2d\u7684\u7f3a\u5931\u503c\u603b\u6570<\/p>\n<p>total_missing_values = df.isnull().sum().sum()<\/p>\n<p>print(total_missing_values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u6570\u503c\uff0c\u8868\u793a\u6574\u4e2aDataFrame\u4e2d\u7f3a\u5931\u503c\u7684\u603b\u6570\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516d\u3001\u53ef\u89c6\u5316\u7f3a\u5931\u503c<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u53ef\u89c6\u5316\u5de5\u5177\u6765\u67e5\u770b\u7f3a\u5931\u503c\u7684\u5206\u5e03\u60c5\u51b5\u3002\u5e38\u7528\u7684\u53ef\u89c6\u5316\u5de5\u5177\u6709<code>matplotlib<\/code>\u548c<code>seaborn<\/code>\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528<code>seaborn<\/code>\u7ed8\u5236\u7f3a\u5931\u503c\u70ed\u56fe<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u7f3a\u5931\u503c\u70ed\u56fe<\/strong><\/h2>\n<p>sns.heatmap(df.isnull(), cbar=False, cmap=&#39;viridis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528<code>missingno<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>missingno<\/code>\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u53ef\u89c6\u5316\u7f3a\u5931\u503c\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import missingno as msno<\/p>\n<h2><strong>\u7ed8\u5236\u7f3a\u5931\u503c\u77e9\u9635<\/strong><\/h2>\n<p>msno.matrix(df)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u7f3a\u5931\u503c\u6761\u5f62\u56fe<\/strong><\/h2>\n<p>msno.bar(df)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<hr>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u6570\u636e\u96c6\u4e2d\u7684\u7f3a\u5931\u503c\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>isnull()<\/code>\u51fd\u6570\u7ed3\u5408<code>sum()<\/code>\u51fd\u6570\u6765\u67e5\u770b\u6bcf\u5217\u7684\u7f3a\u5931\u503c\u603b\u6570\u3002\u6b64\u5916\uff0c<code>info()<\/code>\u51fd\u6570\u53ef\u4ee5\u63d0\u4f9b\u6570\u636e\u6458\u8981\u4fe1\u606f\uff0c<code>notnull()<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u68c0\u67e5\u975e\u7f3a\u5931\u503c\uff0c<code>seaborn<\/code>\u548c<code>missingno<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u53ef\u89c6\u5316\u7f3a\u5931\u503c\u7684\u5206\u5e03\u60c5\u51b5\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5168\u9762\u4e86\u89e3\u6570\u636e\u96c6\u4e2d\u7f3a\u5931\u503c\u7684\u60c5\u51b5\uff0c\u4ece\u800c\u91c7\u53d6\u76f8\u5e94\u7684\u5904\u7406\u63aa\u65bd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u6570\u636e\u6846\u662f\u5426\u5b58\u5728\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u68c0\u67e5\u6570\u636e\u6846\u4e2d\u7684\u7f3a\u5931\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>isnull()<\/code>\u51fd\u6570\u7ed3\u5408<code>sum()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6bcf\u4e00\u5217\u7684\u7f3a\u5931\u503c\u6570\u91cf\u3002\u4f8b\u5982\uff0c<code>df.isnull().sum()<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6bcf\u5217\u7f3a\u5931\u503c\u8ba1\u6570\u7684\u7cfb\u5217\u3002\u5982\u679c\u67d0\u4e00\u5217\u7684\u8ba1\u6570\u5927\u4e8e\u96f6\uff0c\u8868\u793a\u8be5\u5217\u5b58\u5728\u7f3a\u5931\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u6570\u636e\u6846\u4e2d\u7684\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5904\u7406\u7f3a\u5931\u503c\u7684\u65b9\u5f0f\u6709\u5f88\u591a\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u6027\u8d28\u548c\u5206\u6790\u7684\u9700\u6c42\u3002\u53ef\u4ee5\u9009\u62e9\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u6216\u5217\uff0c\u4f7f\u7528<code>dropna()<\/code>\u65b9\u6cd5\u5373\u53ef\u5b8c\u6210\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u7528\u5747\u503c\u3001\u4e2d\u4f4d\u6570\u6216\u8005\u5176\u4ed6\u7edf\u8ba1\u503c\u586b\u5145\u7f3a\u5931\u503c\uff0c\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u5e2e\u52a9\u8bc6\u522b\u548c\u5904\u7406\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u9664\u4e86pandas\uff0c\u5176\u4ed6\u4e00\u4e9b\u5e93\u5982NumPy\u548cScikit-learn\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\u6765\u5904\u7406\u7f3a\u5931\u503c\u3002NumPy\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u6570\u7ec4\u5e76\u68c0\u67e5\u7f3a\u5931\u503c\uff0c\u800cScikit-learn\u5219\u63d0\u4f9b\u4e86\u5404\u79cd\u63d2\u8865\u65b9\u6cd5\u548c\u586b\u5145\u7b56\u7565\uff0c\u9002\u5408\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u4e2d\u5904\u7406\u7f3a\u5931\u6570\u636e\u3002<\/p>\n<p><strong>\u7f3a\u5931\u503c\u5bf9\u6570\u636e\u5206\u6790\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/strong><br \/>\u7f3a\u5931\u503c\u53ef\u80fd\u5bfc\u81f4\u5206\u6790\u7ed3\u679c\u7684\u4e0d\u51c6\u786e\u548c\u504f\u5dee\uff0c\u7279\u522b\u662f\u5728\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u548c\u673a\u5668\u5b66\u4e60\u6a21\u578b\u6784\u5efa\u65f6\u3002\u7f3a\u5931\u7684\u6570\u636e\u53ef\u80fd\u4f1a\u5f71\u54cd\u6a21\u578b\u7684\u8bad\u7ec3\u548c\u9884\u6d4b\u80fd\u529b\u3002\u56e0\u6b64\uff0c\u8bc6\u522b\u548c\u5904\u7406\u7f3a\u5931\u503c\u662f\u6570\u636e\u9884\u5904\u7406\u7684\u91cd\u8981\u73af\u8282\uff0c\u786e\u4fdd\u6570\u636e\u8d28\u91cf\u548c\u5206\u6790\u7ed3\u679c\u7684\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u68c0\u67e5\u662f\u5426\u5b58\u5728\u7f3a\u5931\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528isnull()\u51fd\u6570\u3001notnull()\u51fd\u6570\u3001\u7ed3\u5408sum() [&hellip;]","protected":false},"author":3,"featured_media":1109703,"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\/1109700"}],"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=1109700"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109700\/revisions"}],"predecessor-version":[{"id":1109706,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109700\/revisions\/1109706"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109703"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}