{"id":1176543,"date":"2025-01-15T17:46:02","date_gmt":"2025-01-15T09:46:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176543.html"},"modified":"2025-01-15T17:46:05","modified_gmt":"2025-01-15T09:46:05","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%8e%bb%e6%b8%85%e6%b4%97%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1176543.html","title":{"rendered":"\u5982\u4f55\u7528python\u53bb\u6e05\u6d17\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25111652\/ff9bbe9a-cfa0-4fd5-ac43-0b631af11eae.webp\" alt=\"\u5982\u4f55\u7528python\u53bb\u6e05\u6d17\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u7528Python\u6e05\u6d17\u6570\u636e\u7684\u65b9\u6cd5\u5305\u62ec\u6570\u636e\u52a0\u8f7d\u3001\u6570\u636e\u68c0\u67e5\u3001\u6570\u636e\u6e05\u6d17\u548c\u6570\u636e\u8f6c\u6362\u7b49\u6b65\u9aa4\u3002<\/strong>\u5728\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\uff0c\u5e38\u89c1\u7684\u64cd\u4f5c\u5305\u62ec\u5220\u9664\u7f3a\u5931\u503c\u3001\u586b\u5145\u7f3a\u5931\u503c\u3001\u5220\u9664\u91cd\u590d\u503c\u3001\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3001\u5904\u7406\u5f02\u5e38\u503c\u3001\u6570\u636e\u6807\u51c6\u5316\u548c\u5f52\u4e00\u5316\u7b49\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5176\u4e2d\u7684\u4e00\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6570\u636e\u52a0\u8f7d<\/h3>\n<\/p>\n<p><p>\u5728\u6e05\u6d17\u6570\u636e\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u52a0\u8f7d\u6570\u636e\u3002Python\u4e2d\u5e38\u7528\u7684\u5e93\u662fpandas\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u6587\u4ef6\uff0c\u5982CSV\u3001Excel\u3001SQL\u6570\u636e\u5e93\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6570\u636e\u52a0\u8f7d\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u52a0\u8f7dCSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u52a0\u8f7dExcel\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<h2><strong>\u4eceSQL\u6570\u636e\u5e93\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<p>data = pd.read_sql_query(&#39;SELECT * FROM table_name&#39;, conn)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u68c0\u67e5<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u68c0\u67e5\u662f\u6570\u636e\u6e05\u6d17\u7684\u7b2c\u4e00\u6b65\uff0c\u76ee\u7684\u662f\u4e86\u89e3\u6570\u636e\u7684\u57fa\u672c\u60c5\u51b5\uff0c\u627e\u51fa\u6570\u636e\u4e2d\u53ef\u80fd\u5b58\u5728\u7684\u95ee\u9898\u3002\u5e38\u89c1\u7684\u6570\u636e\u68c0\u67e5\u65b9\u6cd5\u5305\u62ec\u67e5\u770b\u6570\u636e\u7684\u57fa\u672c\u4fe1\u606f\u3001\u7edf\u8ba1\u63cf\u8ff0\u3001\u7f3a\u5931\u503c\u548c\u91cd\u590d\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u67e5\u770b\u6570\u636e\u7684\u57fa\u672c\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>info()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u6570\u636e\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u5305\u62ec\u6bcf\u5217\u7684\u6570\u636e\u7c7b\u578b\u3001\u975e\u7a7a\u503c\u6570\u91cf\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data.info()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u67e5\u770b\u6570\u636e\u7684\u7edf\u8ba1\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>describe()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u6570\u636e\u7684\u7edf\u8ba1\u63cf\u8ff0\u4fe1\u606f\uff0c\u5305\u62ec\u6bcf\u5217\u7684\u8ba1\u6570\u3001\u5e73\u5747\u503c\u3001\u6807\u51c6\u5dee\u3001\u6700\u5c0f\u503c\u3001\u56db\u5206\u4f4d\u6570\u548c\u6700\u5927\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data.describe()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u67e5\u770b\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>isnull().sum()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u6bcf\u5217\u7684\u7f3a\u5931\u503c\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data.isnull().sum()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u67e5\u770b\u91cd\u590d\u503c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>duplicated().sum()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u6570\u636e\u4e2d\u91cd\u590d\u503c\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data.duplicated().sum()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6570\u636e\u6e05\u6d17<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u7684\u76ee\u7684\u662f\u5904\u7406\u6570\u636e\u4e2d\u7684\u5404\u79cd\u95ee\u9898\uff0c\u4f7f\u6570\u636e\u66f4\u52a0\u5e72\u51c0\u548c\u4e00\u81f4\u3002\u5e38\u89c1\u7684\u6570\u636e\u6e05\u6d17\u64cd\u4f5c\u5305\u62ec\u5220\u9664\u7f3a\u5931\u503c\u3001\u586b\u5145\u7f3a\u5931\u503c\u3001\u5220\u9664\u91cd\u590d\u503c\u3001\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u548c\u5904\u7406\u5f02\u5e38\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5220\u9664\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>dropna()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u6216\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/p>\n<p>data = data.dropna()<\/p>\n<h2><strong>\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u5217<\/strong><\/h2>\n<p>data = data.dropna(axis=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u586b\u5145\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u586b\u5145\u7f3a\u5931\u503c\u3002\u5e38\u89c1\u7684\u586b\u5145\u65b9\u6cd5\u5305\u62ec\u586b\u5145\u7279\u5b9a\u503c\u3001\u586b\u5145\u5747\u503c\u3001\u586b\u5145\u4e2d\u4f4d\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u586b\u5145\u7279\u5b9a\u503c<\/p>\n<p>data = data.fillna(0)<\/p>\n<h2><strong>\u586b\u5145\u5747\u503c<\/strong><\/h2>\n<p>data = data.fillna(data.mean())<\/p>\n<h2><strong>\u586b\u5145\u4e2d\u4f4d\u6570<\/strong><\/h2>\n<p>data = data.fillna(data.median())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5220\u9664\u91cd\u590d\u503c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>drop_duplicates()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u91cd\u590d\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = data.drop_duplicates()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>astype()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data[&#39;column_name&#39;] = data[&#39;column_name&#39;].astype(&#39;int&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u5904\u7406\u5f02\u5e38\u503c<\/h4>\n<\/p>\n<p><p>\u5904\u7406\u5f02\u5e38\u503c\u7684\u65b9\u6cd5\u5305\u62ec\u5220\u9664\u5f02\u5e38\u503c\u548c\u66ff\u6362\u5f02\u5e38\u503c\u7b49\u3002\u5e38\u89c1\u7684\u5904\u7406\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u7edf\u8ba1\u65b9\u6cd5\u68c0\u6d4b\u5f02\u5e38\u503c\u548c\u53ef\u89c6\u5316\u65b9\u6cd5\u68c0\u6d4b\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u7edf\u8ba1\u65b9\u6cd5\u68c0\u6d4b\u5f02\u5e38\u503c<\/p>\n<p>Q1 = data[&#39;column_name&#39;].quantile(0.25)<\/p>\n<p>Q3 = data[&#39;column_name&#39;].quantile(0.75)<\/p>\n<p>IQR = Q3 - Q1<\/p>\n<p>data = data[~((data[&#39;column_name&#39;] &lt; (Q1 - 1.5 * IQR)) | (data[&#39;column_name&#39;] &gt; (Q3 + 1.5 * IQR)))]<\/p>\n<h2><strong>\u4f7f\u7528\u53ef\u89c6\u5316\u65b9\u6cd5\u68c0\u6d4b\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>plt.boxplot(data[&#39;column_name&#39;])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6570\u636e\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u8f6c\u6362\u7684\u76ee\u7684\u662f\u5bf9\u6570\u636e\u8fdb\u884c\u89c4\u8303\u5316\u548c\u6807\u51c6\u5316\uff0c\u4f7f\u5176\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002\u5e38\u89c1\u7684\u6570\u636e\u8f6c\u6362\u64cd\u4f5c\u5305\u62ec\u6570\u636e\u6807\u51c6\u5316\u3001\u6570\u636e\u5f52\u4e00\u5316\u3001\u5206\u7bb1\u5904\u7406\u3001\u7279\u5f81\u7f16\u7801\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u6807\u51c6\u5316<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u6807\u51c6\u5316\u662f\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5747\u503c\u4e3a0\u3001\u6807\u51c6\u5dee\u4e3a1\u7684\u5206\u5e03\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u662fz-score\u6807\u51c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import StandardScaler<\/p>\n<p>scaler = StandardScaler()<\/p>\n<p>data[[&#39;column1&#39;, &#39;column2&#39;]] = scaler.fit_transform(data[[&#39;column1&#39;, &#39;column2&#39;]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u5f52\u4e00\u5316<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u5f52\u4e00\u5316\u662f\u5c06\u6570\u636e\u7f29\u653e\u5230\u7279\u5b9a\u7684\u8303\u56f4\u5185\uff0c\u901a\u5e38\u662f0\u52301\u4e4b\u95f4\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u662fMin-Max\u5f52\u4e00\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import MinMaxScaler<\/p>\n<p>scaler = MinMaxScaler()<\/p>\n<p>data[[&#39;column1&#39;, &#39;column2&#39;]] = scaler.fit_transform(data[[&#39;column1&#39;, &#39;column2&#39;]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5206\u7bb1\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5206\u7bb1\u5904\u7406\u662f\u5c06\u8fde\u7eed\u53d8\u91cf\u8f6c\u6362\u4e3a\u79bb\u6563\u53d8\u91cf\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u7b49\u5bbd\u5206\u7bb1\u548c\u7b49\u9891\u5206\u7bb1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b49\u5bbd\u5206\u7bb1<\/p>\n<p>data[&#39;column_bin&#39;] = pd.cut(data[&#39;column_name&#39;], bins=5)<\/p>\n<h2><strong>\u7b49\u9891\u5206\u7bb1<\/strong><\/h2>\n<p>data[&#39;column_bin&#39;] = pd.qcut(data[&#39;column_name&#39;], q=5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u7279\u5f81\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u7279\u5f81\u7f16\u7801\u662f\u5c06\u5206\u7c7b\u53d8\u91cf\u8f6c\u6362\u4e3a\u6570\u503c\u53d8\u91cf\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62econe-hot\u7f16\u7801\u548c\u6807\u7b7e\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># one-hot\u7f16\u7801<\/p>\n<p>data = pd.get_dummies(data, columns=[&#39;category_column&#39;])<\/p>\n<h2><strong>\u6807\u7b7e\u7f16\u7801<\/strong><\/h2>\n<p>from sklearn.preprocessing import LabelEncoder<\/p>\n<p>le = LabelEncoder()<\/p>\n<p>data[&#39;category_column&#39;] = le.fit_transform(data[&#39;category_column&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6570\u636e\u6e05\u6d17\u7684\u7efc\u5408\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u7efc\u5408\u5b9e\u4f8b\u5c55\u793a\u5982\u4f55\u7528Python\u6e05\u6d17\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u7528\u6237\u4fe1\u606f\u7684\u6570\u636e\u96c6\uff0c\u6570\u636e\u96c6\u5305\u542b\u7528\u6237ID\u3001\u59d3\u540d\u3001\u5e74\u9f84\u3001\u6027\u522b\u3001\u6536\u5165\u548c\u6ce8\u518c\u65e5\u671f\u7b49\u4fe1\u606f\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<p>from sklearn.preprocessing import StandardScaler, MinMaxScaler, LabelEncoder<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;user_data.csv&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u6570\u636e\u7684\u57fa\u672c\u4fe1\u606f<\/strong><\/h2>\n<p>data.info()<\/p>\n<h2><strong>\u67e5\u770b\u6570\u636e\u7684\u7edf\u8ba1\u63cf\u8ff0<\/strong><\/h2>\n<p>data.describe()<\/p>\n<h2><strong>\u67e5\u770b\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>data.isnull().sum()<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>data[&#39;age&#39;] = data[&#39;age&#39;].fillna(data[&#39;age&#39;].mean())<\/p>\n<p>data[&#39;income&#39;] = data[&#39;income&#39;].fillna(data[&#39;income&#39;].median())<\/p>\n<h2><strong>\u5220\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>data = data.drop_duplicates()<\/p>\n<h2><strong>\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/strong><\/h2>\n<p>data[&#39;user_id&#39;] = data[&#39;user_id&#39;].astype(&#39;int&#39;)<\/p>\n<p>data[&#39;registration_date&#39;] = pd.to_datetime(data[&#39;registration_date&#39;])<\/p>\n<h2><strong>\u5904\u7406\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>Q1 = data[&#39;income&#39;].quantile(0.25)<\/p>\n<p>Q3 = data[&#39;income&#39;].quantile(0.75)<\/p>\n<p>IQR = Q3 - Q1<\/p>\n<p>data = data[~((data[&#39;income&#39;] &lt; (Q1 - 1.5 * IQR)) | (data[&#39;income&#39;] &gt; (Q3 + 1.5 * IQR)))]<\/p>\n<h2><strong>\u6570\u636e\u6807\u51c6\u5316<\/strong><\/h2>\n<p>scaler = StandardScaler()<\/p>\n<p>data[[&#39;age&#39;, &#39;income&#39;]] = scaler.fit_transform(data[[&#39;age&#39;, &#39;income&#39;]])<\/p>\n<h2><strong>\u6570\u636e\u5f52\u4e00\u5316<\/strong><\/h2>\n<p>scaler = MinMaxScaler()<\/p>\n<p>data[[&#39;age&#39;, &#39;income&#39;]] = scaler.fit_transform(data[[&#39;age&#39;, &#39;income&#39;]])<\/p>\n<h2><strong>\u5206\u7bb1\u5904\u7406<\/strong><\/h2>\n<p>data[&#39;age_bin&#39;] = pd.cut(data[&#39;age&#39;], bins=5)<\/p>\n<h2><strong>\u7279\u5f81\u7f16\u7801<\/strong><\/h2>\n<p>le = LabelEncoder()<\/p>\n<p>data[&#39;gender&#39;] = le.fit_transform(data[&#39;gender&#39;])<\/p>\n<h2><strong>\u67e5\u770b\u6e05\u6d17\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5b8c\u6210\u6570\u636e\u7684\u52a0\u8f7d\u3001\u68c0\u67e5\u3001\u6e05\u6d17\u548c\u8f6c\u6362\u5de5\u4f5c\uff0c\u4f7f\u6570\u636e\u66f4\u52a0\u5e72\u51c0\u548c\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u6df1\u5165\u5730\u4e86\u89e3\u5982\u4f55\u7528Python\u8fdb\u884c\u6570\u636e\u6e05\u6d17\uff0c\u6211\u4eec\u518d\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u7684\u6848\u4f8b\u8fdb\u884c\u5206\u6790\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5ba2\u6237\u4ea4\u6613\u8bb0\u5f55\u7684\u6570\u636e\u96c6\uff0c\u6570\u636e\u96c6\u5305\u542b\u4ea4\u6613ID\u3001\u5ba2\u6237ID\u3001\u4ea4\u6613\u65e5\u671f\u3001\u4ea4\u6613\u91d1\u989d\u548c\u5546\u54c1\u7c7b\u522b\u7b49\u4fe1\u606f\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u8f6c\u6362\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u5206\u6790\u548c\u5efa\u6a21\u3002<\/p>\n<\/p>\n<p><h4>1. \u52a0\u8f7d\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u6570\u636e\u3002\u5047\u8bbe\u6570\u636e\u4fdd\u5b58\u5728\u4e00\u4e2aCSV\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>transactions = pd.read_csv(&#39;transactions.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u67e5\u770b\u6570\u636e\u7684\u57fa\u672c\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u67e5\u770b\u6570\u636e\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u5305\u62ec\u6bcf\u5217\u7684\u6570\u636e\u7c7b\u578b\u3001\u975e\u7a7a\u503c\u6570\u91cf\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transactions.info()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u67e5\u770b\u6570\u636e\u7684\u7edf\u8ba1\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u67e5\u770b\u6570\u636e\u7684\u7edf\u8ba1\u63cf\u8ff0\u4fe1\u606f\uff0c\u5305\u62ec\u6bcf\u5217\u7684\u8ba1\u6570\u3001\u5e73\u5747\u503c\u3001\u6807\u51c6\u5dee\u3001\u6700\u5c0f\u503c\u3001\u56db\u5206\u4f4d\u6570\u548c\u6700\u5927\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transactions.describe()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u67e5\u770b\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u68c0\u67e5\u6bcf\u5217\u7684\u7f3a\u5931\u503c\u6570\u91cf\uff0c\u627e\u51fa\u9700\u8981\u5904\u7406\u7684\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transactions.isnull().sum()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u586b\u5145\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7f3a\u5931\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\uff0c\u6216\u8005\u586b\u5145\u7f3a\u5931\u503c\u3002\u8fd9\u91cc\u6211\u4eec\u9009\u62e9\u586b\u5145\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u586b\u5145\u4ea4\u6613\u91d1\u989d\u7684\u7f3a\u5931\u503c<\/p>\n<p>transactions[&#39;transaction_amount&#39;] = transactions[&#39;transaction_amount&#39;].fillna(transactions[&#39;transaction_amount&#39;].mean())<\/p>\n<h2><strong>\u586b\u5145\u5546\u54c1\u7c7b\u522b\u7684\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>transactions[&#39;product_category&#39;] = transactions[&#39;product_category&#39;].fillna(&#39;Unknown&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6. \u5220\u9664\u91cd\u590d\u503c<\/h4>\n<\/p>\n<p><p>\u68c0\u67e5\u5e76\u5220\u9664\u91cd\u590d\u7684\u4ea4\u6613\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transactions = transactions.drop_duplicates()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7. \u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5c06\u4ea4\u6613\u65e5\u671f\u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b\uff0c\u4ee5\u4fbf\u8fdb\u884c\u65f6\u95f4\u76f8\u5173\u7684\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transactions[&#39;transaction_date&#39;] = pd.to_datetime(transactions[&#39;transaction_date&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8. \u5904\u7406\u5f02\u5e38\u503c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u7edf\u8ba1\u65b9\u6cd5\u68c0\u6d4b\u5e76\u5904\u7406\u4ea4\u6613\u91d1\u989d\u4e2d\u7684\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">Q1 = transactions[&#39;transaction_amount&#39;].quantile(0.25)<\/p>\n<p>Q3 = transactions[&#39;transaction_amount&#39;].quantile(0.75)<\/p>\n<p>IQR = Q3 - Q1<\/p>\n<p>transactions = transactions[~((transactions[&#39;transaction_amount&#39;] &lt; (Q1 - 1.5 * IQR)) | (transactions[&#39;transaction_amount&#39;] &gt; (Q3 + 1.5 * IQR)))]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>9. \u6570\u636e\u6807\u51c6\u5316<\/h4>\n<\/p>\n<p><p>\u5bf9\u4ea4\u6613\u91d1\u989d\u8fdb\u884c\u6807\u51c6\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import StandardScaler<\/p>\n<p>scaler = StandardScaler()<\/p>\n<p>transactions[[&#39;transaction_amount&#39;]] = scaler.fit_transform(transactions[[&#39;transaction_amount&#39;]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>10. \u6570\u636e\u5f52\u4e00\u5316<\/h4>\n<\/p>\n<p><p>\u5bf9\u4ea4\u6613\u91d1\u989d\u8fdb\u884c\u5f52\u4e00\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import MinMaxScaler<\/p>\n<p>scaler = MinMaxScaler()<\/p>\n<p>transactions[[&#39;transaction_amount&#39;]] = scaler.fit_transform(transactions[[&#39;transaction_amount&#39;]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>11. \u5206\u7bb1\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5bf9\u4ea4\u6613\u91d1\u989d\u8fdb\u884c\u5206\u7bb1\u5904\u7406\uff0c\u5c06\u8fde\u7eed\u53d8\u91cf\u8f6c\u6362\u4e3a\u79bb\u6563\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transactions[&#39;transaction_amount_bin&#39;] = pd.cut(transactions[&#39;transaction_amount&#39;], bins=5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>12. \u7279\u5f81\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u5c06\u5546\u54c1\u7c7b\u522b\u8fdb\u884cone-hot\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transactions = pd.get_dummies(transactions, columns=[&#39;product_category&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>13. \u67e5\u770b\u6e05\u6d17\u540e\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u67e5\u770b\u6e05\u6d17\u540e\u7684\u6570\u636e\uff0c\u786e\u4fdd\u6570\u636e\u6e05\u6d17\u548c\u8f6c\u6362\u5de5\u4f5c\u6b63\u786e\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(transactions.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u5b8c\u6210\u4e86\u5ba2\u6237\u4ea4\u6613\u8bb0\u5f55\u6570\u636e\u7684\u6e05\u6d17\u548c\u8f6c\u6362\u5de5\u4f5c\u3002\u8fd9\u4e9b\u6b65\u9aa4\u5305\u62ec\u52a0\u8f7d\u6570\u636e\u3001\u68c0\u67e5\u6570\u636e\u3001\u586b\u5145\u7f3a\u5931\u503c\u3001\u5220\u9664\u91cd\u590d\u503c\u3001\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3001\u5904\u7406\u5f02\u5e38\u503c\u3001\u6807\u51c6\u5316\u3001\u5f52\u4e00\u5316\u3001\u5206\u7bb1\u5904\u7406\u548c\u7279\u5f81\u7f16\u7801\u7b49\u3002\u6e05\u6d17\u540e\u7684\u6570\u636e\u66f4\u52a0\u5e72\u51c0\u548c\u4e00\u81f4\uff0c\u53ef\u4ee5\u7528\u4e8e\u540e\u7eed\u7684\u5206\u6790\u548c\u5efa\u6a21\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u662f\u6570\u636e\u5206\u6790\u548c\u5efa\u6a21\u8fc7\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\u3002\u901a\u8fc7\u4f7f\u7528Python\u4e2d\u7684pandas\u3001numpy\u548cscikit-learn\u7b49\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u52a0\u8f7d\u3001\u6570\u636e\u68c0\u67e5\u3001\u6570\u636e\u6e05\u6d17\u548c\u6570\u636e\u8f6c\u6362\u5de5\u4f5c\u3002\u5e38\u89c1\u7684\u6570\u636e\u6e05\u6d17\u64cd\u4f5c\u5305\u62ec\u5220\u9664\u7f3a\u5931\u503c\u3001\u586b\u5145\u7f3a\u5931\u503c\u3001\u5220\u9664\u91cd\u590d\u503c\u3001\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3001\u5904\u7406\u5f02\u5e38\u503c\u3001\u6570\u636e\u6807\u51c6\u5316\u3001\u6570\u636e\u5f52\u4e00\u5316\u3001\u5206\u7bb1\u5904\u7406\u548c\u7279\u5f81\u7f16\u7801\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u6839\u636e\u6570\u636e\u7684\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u6e05\u6d17\u65b9\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u5927\u5bb6\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u6570\u636e\u6e05\u6d17\u7684\u57fa\u672c\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u4e3a\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5efa\u6a21\u6253\u4e0b\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u8fdb\u884c\u6570\u636e\u6e05\u6d17\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u975e\u5e38\u91cd\u8981\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecPandas\u3001NumPy\u548cBeautiful Soup\u3002Pandas\u975e\u5e38\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff1bNumPy\u5219\u5728\u5904\u7406\u6570\u503c\u6570\u636e\u65f6\u8868\u73b0\u4f18\u5f02\uff1b\u800cBeautiful Soup\u5219\u9002\u7528\u4e8e\u7f51\u9875\u6570\u636e\u7684\u63d0\u53d6\u548c\u6e05\u6d17\u3002\u6839\u636e\u6570\u636e\u7684\u7c7b\u578b\u548c\u9700\u6c42\uff0c\u5408\u7406\u9009\u62e9\u5e93\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6570\u636e\u6e05\u6d17\u7684\u6548\u7387\u3002<\/p>\n<p><strong>\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u6311\u6218\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728\u6570\u636e\u6e05\u6d17\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7528\u6237\u53ef\u80fd\u4f1a\u9047\u5230\u7f3a\u5931\u503c\u3001\u91cd\u590d\u6570\u636e\u3001\u5f02\u5e38\u503c\u548c\u4e0d\u4e00\u81f4\u7684\u6570\u636e\u683c\u5f0f\u7b49\u6311\u6218\u3002\u7f3a\u5931\u503c\u53ef\u80fd\u5bfc\u81f4\u5206\u6790\u7ed3\u679c\u7684\u4e0d\u51c6\u786e\uff1b\u91cd\u590d\u6570\u636e\u4f1a\u5f15\u53d1\u7edf\u8ba1\u504f\u5dee\uff1b\u5f02\u5e38\u503c\u5219\u53ef\u80fd\u6e90\u4e8e\u6570\u636e\u5f55\u5165\u9519\u8bef\u6216\u7cfb\u7edf\u6545\u969c\uff1b\u4e0d\u4e00\u81f4\u7684\u6570\u636e\u683c\u5f0f\u4f1a\u589e\u52a0\u5904\u7406\u590d\u6742\u6027\u3002\u4e86\u89e3\u8fd9\u4e9b\u6311\u6218\u5e76\u91c7\u53d6\u76f8\u5e94\u63aa\u65bd\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u6570\u636e\u7684\u8d28\u91cf\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u6570\u636e\u6e05\u6d17\u7684\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u9075\u5faa\uff1f<\/strong><br \/>\u6709\u6548\u7684\u6570\u636e\u6e05\u6d17\u53ef\u4ee5\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u3002\u9996\u5148\uff0c\u59cb\u7ec8\u5907\u4efd\u539f\u59cb\u6570\u636e\uff0c\u4ee5\u9632\u6b62\u610f\u5916\u4e22\u5931\u3002\u5176\u6b21\uff0c\u6e05\u6d17\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u521d\u6b65\u63a2\u7d22\uff0c\u4e86\u89e3\u6570\u636e\u7684\u57fa\u672c\u60c5\u51b5\u548c\u95ee\u9898\u3002\u63a5\u7740\uff0c\u9010\u6b65\u5904\u7406\u7f3a\u5931\u503c\u3001\u91cd\u590d\u6570\u636e\u548c\u5f02\u5e38\u503c\uff0c\u540c\u65f6\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002\u6700\u540e\uff0c\u4fdd\u6301\u6e05\u6d17\u8fc7\u7a0b\u7684\u8bb0\u5f55\uff0c\u4ee5\u4fbf\u4e8e\u672a\u6765\u7684\u5ba1\u8ba1\u548c\u590d\u67e5\u3002\u8fd9\u4e9b\u5b9e\u8df5\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u6570\u636e\u6e05\u6d17\u5de5\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u6e05\u6d17\u6570\u636e\u7684\u65b9\u6cd5\u5305\u62ec\u6570\u636e\u52a0\u8f7d\u3001\u6570\u636e\u68c0\u67e5\u3001\u6570\u636e\u6e05\u6d17\u548c\u6570\u636e\u8f6c\u6362\u7b49\u6b65\u9aa4\u3002\u5728\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\uff0c\u5e38\u89c1\u7684\u64cd\u4f5c\u5305\u62ec\u5220\u9664 [&hellip;]","protected":false},"author":3,"featured_media":1176551,"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\/1176543"}],"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=1176543"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176543\/revisions"}],"predecessor-version":[{"id":1176555,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176543\/revisions\/1176555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1176551"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1176543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1176543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1176543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}