{"id":1097635,"date":"2025-01-08T15:13:41","date_gmt":"2025-01-08T07:13:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097635.html"},"modified":"2025-01-08T15:13:43","modified_gmt":"2025-01-08T07:13:43","slug":"python%e4%b8%ad%e6%95%b0%e6%8d%ae%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e6%a3%80%e9%aa%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097635.html","title":{"rendered":"Python\u4e2d\u6570\u636e\u5982\u4f55\u8fdb\u884c\u68c0\u9a8c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212314\/d84f663e-95e7-449f-8c4c-8f6e9e42884c.webp\" alt=\"Python\u4e2d\u6570\u636e\u5982\u4f55\u8fdb\u884c\u68c0\u9a8c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6570\u636e\u68c0\u9a8c\u662f\u786e\u4fdd\u6570\u636e\u8d28\u91cf\u548c\u5b8c\u6574\u6027\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u6570\u636e\u7c7b\u578b\u68c0\u67e5\u3001\u7f3a\u5931\u503c\u5904\u7406\u3001\u91cd\u590d\u503c\u5904\u7406\u3001\u5f02\u5e38\u503c\u68c0\u6d4b\u548c\u6570\u636e\u4e00\u81f4\u6027\u68c0\u67e5\u3002<\/strong>\u5176\u4e2d\uff0c\u6570\u636e\u7c7b\u578b\u68c0\u67e5\u662f\u6700\u57fa\u7840\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u786e\u4fdd\u6bcf\u4e2a\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u6b63\u786e\u662f\u540e\u7eed\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u524d\u63d0\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6570\u636e\u68c0\u9a8c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6570\u636e\u7c7b\u578b\u68c0\u67e5<\/p>\n<\/p>\n<p><p>\u6570\u636e\u7c7b\u578b\u68c0\u67e5\u662f\u6570\u636e\u68c0\u9a8c\u7684\u7b2c\u4e00\u6b65\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u6b63\u786e\u662f\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u57fa\u7840\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u5b8c\u6210\u8fd9\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>print(df.dtypes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u68c0\u67e5\u6570\u636e\u7c7b\u578b\uff0c\u53d1\u73b0\u4e0d\u7b26\u5408\u9884\u671f\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5bf9\u8c61\u7c7b\u578b\u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;date_column&#39;] = pd.to_datetime(df[&#39;date_column&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u7f3a\u5931\u503c\u5904\u7406<\/p>\n<\/p>\n<p><p>\u7f3a\u5931\u503c\u662f\u6570\u636e\u96c6\u4e2d\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u5904\u7406\u7f3a\u5931\u503c\u7684\u65b9\u5f0f\u5305\u62ec\u5220\u9664\u3001\u586b\u5145\u7b49\u3002\u9996\u5148\u9700\u8981\u68c0\u67e5\u6570\u636e\u4e2d\u7684\u7f3a\u5931\u503c\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u7f3a\u5931\u503c<\/p>\n<p>print(df.isnull().sum())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7f3a\u5931\u503c\u7684\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">df.dropna(inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u586b\u5145\u7f3a\u5931\u503c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5747\u503c\u586b\u5145<\/p>\n<p>df[&#39;column_name&#39;].fillna(df[&#39;column_name&#39;].mean(), inplace=True)<\/p>\n<h2><strong>\u4f7f\u7528\u4e2d\u4f4d\u6570\u586b\u5145<\/strong><\/h2>\n<p>df[&#39;column_name&#39;].fillna(df[&#39;column_name&#39;].median(), inplace=True)<\/p>\n<h2><strong>\u4f7f\u7528\u4f17\u6570\u586b\u5145<\/strong><\/h2>\n<p>df[&#39;column_name&#39;].fillna(df[&#39;column_name&#39;].mode()[0], inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u91cd\u590d\u503c\u5904\u7406<\/p>\n<\/p>\n<p><p>\u91cd\u590d\u503c\u4f1a\u5f71\u54cd\u6570\u636e\u5206\u6790\u7684\u51c6\u786e\u6027\uff0c\u9700\u8981\u8fdb\u884c\u68c0\u67e5\u548c\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u91cd\u590d\u503c<\/p>\n<p>print(df.duplicated().sum())<\/p>\n<h2><strong>\u5220\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>df.drop_duplicates(inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5f02\u5e38\u503c\u68c0\u6d4b<\/p>\n<\/p>\n<p><p>\u5f02\u5e38\u503c\u53ef\u80fd\u662f\u6570\u636e\u5f55\u5165\u9519\u8bef\u6216\u771f\u5b9e\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u5e38\u7528\u7684\u5f02\u5e38\u503c\u68c0\u6d4b\u65b9\u6cd5\u5305\u62ec\u7bb1\u7ebf\u56fe\u548cZ\u5206\u6570\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u7bb1\u7ebf\u56fe\u68c0\u6d4b\u5f02\u5e38\u503c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u7bb1\u7ebf\u56fe<\/strong><\/h2>\n<p>df.boxplot(column=[&#39;column_name&#39;])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Z\u5206\u6570\u68c0\u6d4b\u5f02\u5e38\u503c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from scipy import stats<\/p>\n<h2><strong>\u8ba1\u7b97Z\u5206\u6570<\/strong><\/h2>\n<p>df[&#39;z_score&#39;] = stats.zscore(df[&#39;column_name&#39;])<\/p>\n<h2><strong>\u8fc7\u6ee4\u51fa\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>outliers = df[df[&#39;z_score&#39;].abs() &gt; 3]<\/p>\n<p>print(outliers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6570\u636e\u4e00\u81f4\u6027\u68c0\u67e5<\/p>\n<\/p>\n<p><p>\u6570\u636e\u4e00\u81f4\u6027\u68c0\u67e5\u786e\u4fdd\u6570\u636e\u5728\u4e0d\u540c\u5217\u6216\u8868\u4e4b\u95f4\u7684\u4e00\u81f4\u6027\u3002\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u89c4\u5219\u6765\u68c0\u67e5\u6570\u636e\u4e00\u81f4\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u81ea\u5b9a\u4e49\u4e00\u81f4\u6027\u68c0\u67e5\u51fd\u6570<\/p>\n<p>def check_consistency(row):<\/p>\n<p>    if row[&#39;start_date&#39;] &gt; row[&#39;end_date&#39;]:<\/p>\n<p>        return False<\/p>\n<p>    return True<\/p>\n<h2><strong>\u5e94\u7528\u4e00\u81f4\u6027\u68c0\u67e5\u51fd\u6570<\/strong><\/h2>\n<p>df[&#39;consistent&#39;] = df.apply(check_consistency, axis=1)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u4e0d\u4e00\u81f4\u7684\u6570\u636e<\/strong><\/h2>\n<p>inconsistent_data = df[~df[&#39;consistent&#39;]]<\/p>\n<p>print(inconsistent_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5168\u9762\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u68c0\u9a8c\uff0c\u786e\u4fdd\u6570\u636e\u7684\u8d28\u91cf\u548c\u5b8c\u6574\u6027\u3002\u4e0b\u9762\u662f\u5bf9\u6bcf\u4e2a\u6b65\u9aa4\u7684\u8be6\u7ec6\u63cf\u8ff0\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6570\u636e\u7c7b\u578b\u68c0\u67e5<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u7c7b\u578b\u68c0\u67e5\u662f\u6570\u636e\u68c0\u9a8c\u7684\u57fa\u7840\u6b65\u9aa4\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u6b63\u786e\u662f\u540e\u7eed\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u524d\u63d0\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u5b8c\u6210\u8fd9\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u6570\u636e\u7c7b\u578b\u9519\u8bef\u53ef\u80fd\u5bfc\u81f4\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u51fa\u73b0\u95ee\u9898\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u636e\u5728\u6570\u503c\u8fd0\u7b97\u4e2d\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>print(df.dtypes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u68c0\u67e5\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u53d1\u73b0\u5e76\u4fee\u6b63\u4e0d\u7b26\u5408\u9884\u671f\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5bf9\u8c61\u7c7b\u578b\u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;date_column&#39;] = pd.to_datetime(df[&#39;date_column&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f3a\u5931\u503c\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u7f3a\u5931\u503c\u662f\u6570\u636e\u96c6\u4e2d\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u5904\u7406\u7f3a\u5931\u503c\u7684\u65b9\u5f0f\u5305\u62ec\u5220\u9664\u3001\u586b\u5145\u7b49\u3002\u9996\u5148\u9700\u8981\u68c0\u67e5\u6570\u636e\u4e2d\u7684\u7f3a\u5931\u503c\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u7f3a\u5931\u503c<\/p>\n<p>print(df.isnull().sum())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7f3a\u5931\u503c\u7684\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">df.dropna(inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u586b\u5145\u7f3a\u5931\u503c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5747\u503c\u586b\u5145<\/p>\n<p>df[&#39;column_name&#39;].fillna(df[&#39;column_name&#39;].mean(), inplace=True)<\/p>\n<h2><strong>\u4f7f\u7528\u4e2d\u4f4d\u6570\u586b\u5145<\/strong><\/h2>\n<p>df[&#39;column_name&#39;].fillna(df[&#39;column_name&#39;].median(), inplace=True)<\/p>\n<h2><strong>\u4f7f\u7528\u4f17\u6570\u586b\u5145<\/strong><\/h2>\n<p>df[&#39;column_name&#39;].fillna(df[&#39;column_name&#39;].mode()[0], inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u91cd\u590d\u503c\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u91cd\u590d\u503c\u4f1a\u5f71\u54cd\u6570\u636e\u5206\u6790\u7684\u51c6\u786e\u6027\uff0c\u9700\u8981\u8fdb\u884c\u68c0\u67e5\u548c\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u91cd\u590d\u503c<\/p>\n<p>print(df.duplicated().sum())<\/p>\n<h2><strong>\u5220\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>df.drop_duplicates(inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5f02\u5e38\u503c\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>\u5f02\u5e38\u503c\u53ef\u80fd\u662f\u6570\u636e\u5f55\u5165\u9519\u8bef\u6216\u771f\u5b9e\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u5e38\u7528\u7684\u5f02\u5e38\u503c\u68c0\u6d4b\u65b9\u6cd5\u5305\u62ec\u7bb1\u7ebf\u56fe\u548cZ\u5206\u6570\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u7bb1\u7ebf\u56fe\u68c0\u6d4b\u5f02\u5e38\u503c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u7bb1\u7ebf\u56fe<\/strong><\/h2>\n<p>df.boxplot(column=[&#39;column_name&#39;])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Z\u5206\u6570\u68c0\u6d4b\u5f02\u5e38\u503c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from scipy import stats<\/p>\n<h2><strong>\u8ba1\u7b97Z\u5206\u6570<\/strong><\/h2>\n<p>df[&#39;z_score&#39;] = stats.zscore(df[&#39;column_name&#39;])<\/p>\n<h2><strong>\u8fc7\u6ee4\u51fa\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>outliers = df[df[&#39;z_score&#39;].abs() &gt; 3]<\/p>\n<p>print(outliers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6570\u636e\u4e00\u81f4\u6027\u68c0\u67e5<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u4e00\u81f4\u6027\u68c0\u67e5\u786e\u4fdd\u6570\u636e\u5728\u4e0d\u540c\u5217\u6216\u8868\u4e4b\u95f4\u7684\u4e00\u81f4\u6027\u3002\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u89c4\u5219\u6765\u68c0\u67e5\u6570\u636e\u4e00\u81f4\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u81ea\u5b9a\u4e49\u4e00\u81f4\u6027\u68c0\u67e5\u51fd\u6570<\/p>\n<p>def check_consistency(row):<\/p>\n<p>    if row[&#39;start_date&#39;] &gt; row[&#39;end_date&#39;]:<\/p>\n<p>        return False<\/p>\n<p>    return True<\/p>\n<h2><strong>\u5e94\u7528\u4e00\u81f4\u6027\u68c0\u67e5\u51fd\u6570<\/strong><\/h2>\n<p>df[&#39;consistent&#39;] = df.apply(check_consistency, axis=1)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u4e0d\u4e00\u81f4\u7684\u6570\u636e<\/strong><\/h2>\n<p>inconsistent_data = df[~df[&#39;consistent&#39;]]<\/p>\n<p>print(inconsistent_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6570\u636e\u4e00\u81f4\u6027\u68c0\u67e5\u9700\u8981\u6839\u636e\u5177\u4f53\u4e1a\u52a1\u903b\u8f91\u5b9a\u5236\uff0c\u4f8b\u5982\u5728\u8d22\u52a1\u6570\u636e\u4e2d\uff0c\u9700\u8981\u786e\u4fdd\u501f\u8d37\u5e73\u8861\uff1b\u5728\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u4e2d\uff0c\u9700\u8981\u786e\u4fdd\u65f6\u95f4\u7684\u5148\u540e\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u5b9e\u9645\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u6848\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6570\u636e\u68c0\u9a8c\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5458\u5de5\u4fe1\u606f\u7684\u6570\u636e\u96c6\uff0c\u6570\u636e\u96c6\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n<\/p>\n<ul>\n<li>employee_id: \u5458\u5de5\u7f16\u53f7<\/li>\n<li>name: \u5458\u5de5\u59d3\u540d<\/li>\n<li>department: \u6240\u5728\u90e8\u95e8<\/li>\n<li>hire_date: \u5165\u804c\u65e5\u671f<\/li>\n<li>salary: \u85aa\u6c34<\/li>\n<\/ul>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u8bfb\u53d6\u6570\u636e\u5e76\u68c0\u67e5\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;employee_data.csv&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>print(df.dtypes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53d1\u73b0hire_date\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u662f\u5bf9\u8c61\u7c7b\u578b\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;hire_date&#39;] = pd.to_datetime(df[&#39;hire_date&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u68c0\u67e5\u6570\u636e\u4e2d\u7684\u7f3a\u5931\u503c\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u7f3a\u5931\u503c<\/p>\n<p>print(df.isnull().sum())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5047\u8bbesalary\u5b57\u6bb5\u5b58\u5728\u7f3a\u5931\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5747\u503c\u586b\u5145\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;salary&#39;].fillna(df[&#39;salary&#39;].mean(), inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u68c0\u67e5\u6570\u636e\u4e2d\u7684\u91cd\u590d\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u91cd\u590d\u503c<\/p>\n<p>print(df.duplicated().sum())<\/p>\n<h2><strong>\u5220\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>df.drop_duplicates(inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u7bb1\u7ebf\u56fe\u548cZ\u5206\u6570\u68c0\u6d4bsalary\u5b57\u6bb5\u7684\u5f02\u5e38\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from scipy import stats<\/p>\n<h2><strong>\u7ed8\u5236\u7bb1\u7ebf\u56fe<\/strong><\/h2>\n<p>df.boxplot(column=[&#39;salary&#39;])<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u8ba1\u7b97Z\u5206\u6570<\/strong><\/h2>\n<p>df[&#39;z_score&#39;] = stats.zscore(df[&#39;salary&#39;])<\/p>\n<h2><strong>\u8fc7\u6ee4\u51fa\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>outliers = df[df[&#39;z_score&#39;].abs() &gt; 3]<\/p>\n<p>print(outliers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u68c0\u67e5\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002\u4f8b\u5982\uff0c\u786e\u4fddhire_date\u5b57\u6bb5\u4e2d\u7684\u65e5\u671f\u4e0d\u665a\u4e8e\u5f53\u524d\u65e5\u671f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u4e00\u81f4\u6027\u68c0\u67e5\u51fd\u6570<\/strong><\/h2>\n<p>def check_consistency(row):<\/p>\n<p>    if row[&#39;hire_date&#39;] &gt; datetime.datetime.now():<\/p>\n<p>        return False<\/p>\n<p>    return True<\/p>\n<h2><strong>\u5e94\u7528\u4e00\u81f4\u6027\u68c0\u67e5\u51fd\u6570<\/strong><\/h2>\n<p>df[&#39;consistent&#39;] = df.apply(check_consistency, axis=1)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u4e0d\u4e00\u81f4\u7684\u6570\u636e<\/strong><\/h2>\n<p>inconsistent_data = df[~df[&#39;consistent&#39;]]<\/p>\n<p>print(inconsistent_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5168\u9762\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u68c0\u9a8c\uff0c\u786e\u4fdd\u6570\u636e\u7684\u8d28\u91cf\u548c\u5b8c\u6574\u6027\u3002\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u4e1a\u52a1\u9700\u6c42\u548c\u6570\u636e\u7279\u70b9\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u66f4\u591a\u7684\u5b9a\u5236\u5316\u68c0\u9a8c\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u68c0\u9a8c\u662f\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u5fc5\u4e0d\u53ef\u5c11\u7684\u4e00\u6b65\uff0c\u901a\u8fc7\u6570\u636e\u7c7b\u578b\u68c0\u67e5\u3001\u7f3a\u5931\u503c\u5904\u7406\u3001\u91cd\u590d\u503c\u5904\u7406\u3001\u5f02\u5e38\u503c\u68c0\u6d4b\u548c\u6570\u636e\u4e00\u81f4\u6027\u68c0\u67e5\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u8d28\u91cf\u548c\u5b8c\u6574\u6027\u3002\u5728Python\u4e2d\uff0c\u501f\u52a9pandas\u7b49\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b8c\u6210\u8fd9\u4e9b\u6b65\u9aa4\u3002\u901a\u8fc7\u5b9e\u9645\u6848\u4f8b\u7684\u6f14\u793a\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u6570\u636e\u68c0\u9a8c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u6570\u636e\u68c0\u9a8c\u65b9\u6cd5\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u68c0\u9a8c\u65b9\u6cd5\uff0c\u5305\u62ec\u7edf\u8ba1\u68c0\u9a8c\u3001\u7f3a\u5931\u503c\u68c0\u6d4b\u548c\u6570\u636e\u5206\u5e03\u68c0\u9a8c\u3002\u5e38\u89c1\u7684\u7edf\u8ba1\u68c0\u9a8c\u65b9\u6cd5\u6709t\u68c0\u9a8c\u3001\u5361\u65b9\u68c0\u9a8c\u548cANOVA\u7b49\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528SciPy\u5e93\u8fdb\u884c\u5b9e\u73b0\u3002\u6b64\u5916\uff0cPandas\u5e93\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u7f3a\u5931\u503c\u548c\u91cd\u590d\u6570\u636e\uff0c\u800cSeaborn\u548cMatplotlib\u5219\u53ef\u4ee5\u5e2e\u52a9\u53ef\u89c6\u5316\u6570\u636e\u5206\u5e03\uff0c\u65b9\u4fbf\u5224\u65ad\u6570\u636e\u7684\u6b63\u6001\u6027\u7b49\u7279\u5f81\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u68c0\u67e5\u6570\u636e\u7684\u5b8c\u6574\u6027\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>isnull()<\/code>\u548c<code>dropna()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u548c\u5904\u7406\u7f3a\u5931\u503c\u3002<code>isnull()<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u54ea\u4e9b\u6570\u636e\u7f3a\u5931\uff0c\u800c<code>dropna()<\/code>\u5219\u53ef\u4ee5\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u6216\u5217\u3002\u6b64\u5916\uff0c<code>fillna()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u7528\u7279\u5b9a\u503c\u586b\u5145\u7f3a\u5931\u7684\u6570\u636e\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6570\u636e\u7684\u5206\u5e03\u68c0\u9a8c\uff1f<\/strong><br \/>\u8fdb\u884c\u6570\u636e\u5206\u5e03\u68c0\u9a8c\u65f6\uff0c\u5e38\u7528\u7684\u5de5\u5177\u662fSeaborn\u548cScipy\u5e93\u3002\u901a\u8fc7Seaborn\u7684<code>distplot()<\/code>\u6216<code>histplot()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u53ef\u89c6\u5316\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\uff0c\u5e2e\u52a9\u5224\u65ad\u5176\u662f\u5426\u7b26\u5408\u6b63\u6001\u5206\u5e03\u3002\u6b64\u5916\uff0cScipy\u7684<code>shapiro()<\/code>\u51fd\u6570\u53ef\u4ee5\u8fdb\u884cShapiro-Wilk\u68c0\u9a8c\uff0c\u4ee5\u5224\u65ad\u6570\u636e\u662f\u5426\u7b26\u5408\u6b63\u6001\u5206\u5e03\u3002\u5176\u4ed6\u65b9\u6cd5\u5982Kolmogorov-Smirnov\u68c0\u9a8c\u4e5f\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u66f4\u6df1\u5165\u7684\u5206\u5e03\u68c0\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6570\u636e\u68c0\u9a8c\u662f\u786e\u4fdd\u6570\u636e\u8d28\u91cf\u548c\u5b8c\u6574\u6027\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u6570\u636e\u7c7b\u578b\u68c0\u67e5\u3001\u7f3a\u5931\u503c\u5904\u7406\u3001\u91cd\u590d\u503c\u5904\u7406 [&hellip;]","protected":false},"author":3,"featured_media":1097638,"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\/1097635"}],"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=1097635"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097635\/revisions"}],"predecessor-version":[{"id":1097639,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097635\/revisions\/1097639"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097638"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}