{"id":1183833,"date":"2025-01-15T19:19:10","date_gmt":"2025-01-15T11:19:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183833.html"},"modified":"2025-01-15T19:19:13","modified_gmt":"2025-01-15T11:19:13","slug":"python%e5%a6%82%e4%bd%95%e9%80%89%e5%8f%96%e7%9b%b8%e5%ba%94%e7%9a%84%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183833.html","title":{"rendered":"python\u5982\u4f55\u9009\u53d6\u76f8\u5e94\u7684\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133446\/45cd168a-31a8-42a8-9228-975ca4edb61e.webp\" alt=\"python\u5982\u4f55\u9009\u53d6\u76f8\u5e94\u7684\u5217\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u9009\u53d6\u76f8\u5e94\u7684\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001\u901a\u8fc7\u5217\u540d\u6216\u5217\u7d22\u5f15\u8fdb\u884c\u9009\u62e9\u3001\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u3002<\/strong> \u5176\u4e2d\u4f7f\u7528pandas\u5e93\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u63a5\u4e0b\u6765\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528pandas\u5e93\u6765\u9009\u53d6\u7279\u5b9a\u7684\u5217\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u5e93\uff1a<\/strong> pandas\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u5904\u7406\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u9009\u53d6\u6570\u636e\u6846\u4e2d\u7684\u7279\u5b9a\u5217\u3002\u4f7f\u7528pandas\u5e93\u9009\u53d6\u5217\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u901a\u8fc7\u5217\u540d\u9009\u62e9\u5217\uff1a<\/strong> \u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7[]\u5e76\u4f20\u5165\u5217\u540d\u5b57\u7b26\u4e32\u6765\u9009\u62e9\u5355\u5217\uff0c\u6216\u8005\u4f20\u5165\u5305\u542b\u591a\u4e2a\u5217\u540d\u7684\u5217\u8868\u6765\u9009\u62e9\u591a\u5217\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u9009\u62e9\u5355\u5217<\/strong><\/h2>\n<p>column_a = df[&#39;A&#39;]<\/p>\n<h2><strong>\u9009\u62e9\u591a\u5217<\/strong><\/h2>\n<p>columns_ab = df[[&#39;A&#39;, &#39;B&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5217\u540d\u9009\u62e9\u5217\u7684\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u5df2\u77e5\u5217\u540d\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001PANDAS\u5e93\u7b80\u4ecb<\/h2>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u6570\u636e\u7ed3\u6784\u5982Series\u548cDataFrame\u3002\u5b83\u80fd\u591f\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u6e90\uff0c\u5305\u62ecCSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u3001SQL\u6570\u636e\u5e93\u7b49\u3002pandas\u5177\u6709\u4e30\u5bcc\u7684\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u80fd\u591f\u5b8c\u6210\u6570\u636e\u6e05\u6d17\u3001\u53d8\u6362\u3001\u5408\u5e76\u3001\u5206\u7ec4\u7b49\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165pandas<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528pandas\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u521b\u5efaDataFrame<\/h3>\n<\/p>\n<p><p>DataFrame\u662fpandas\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u5b83\u7c7b\u4f3c\u4e8eExcel\u4e2d\u7684\u7535\u5b50\u8868\u683c\u3002\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u3001\u5217\u8868\u3001NumPy\u6570\u7ec4\u7b49\u591a\u79cd\u65b9\u5f0f\u521b\u5efaDataFrame\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u8bfb\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>pandas\u63d0\u4f9b\u4e86\u8bfb\u53d6\u591a\u79cd\u6570\u636e\u6e90\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u8bfb\u53d6CSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u3001SQL\u6570\u636e\u5e93\u7b49\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6CSV\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u901a\u8fc7\u5217\u540d\u9009\u62e9\u5217<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u5217\u540d\u9009\u62e9\u5217\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002\u53ef\u4ee5\u4f7f\u7528DataFrame\u5bf9\u8c61\u7684\u65b9\u62ec\u53f7[]\u64cd\u4f5c\u7b26\uff0c\u5e76\u4f20\u5165\u5217\u540d\u5b57\u7b26\u4e32\u6216\u5305\u542b\u591a\u4e2a\u5217\u540d\u7684\u5217\u8868\u6765\u9009\u62e9\u7279\u5b9a\u7684\u5217\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u9009\u62e9\u5355\u5217<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5355\u5217\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165\u5217\u540d\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column_a = df[&#39;A&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aSeries\u5bf9\u8c61\uff0c\u5305\u542b\u5217&#39;A&#39;\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u9009\u62e9\u591a\u5217<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u591a\u5217\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165\u5305\u542b\u591a\u4e2a\u5217\u540d\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">columns_ab = df[[&#39;A&#39;, &#39;B&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u5305\u542b\u5217&#39;A&#39;\u548c&#39;B&#39;\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u9009\u62e9\u5217\u5e76\u8fdb\u884c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5217\u540e\uff0c\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u8ba1\u7b97\u5747\u503c\u3001\u6c42\u548c\u3001\u6392\u5e8f\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mean_a = df[&#39;A&#39;].mean()<\/p>\n<p>sum_b = df[&#39;B&#39;].sum()<\/p>\n<p>sorted_df = df[[&#39;A&#39;, &#39;B&#39;]].sort_values(by=&#39;A&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u901a\u8fc7\u5217\u7d22\u5f15\u9009\u62e9\u5217<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u901a\u8fc7\u5217\u540d\u9009\u62e9\u5217\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5217\u7d22\u5f15\u9009\u62e9\u5217\u3002\u5217\u7d22\u5f15\u662f\u5217\u5728DataFrame\u4e2d\u7684\u4f4d\u7f6e\uff0c\u4ece0\u5f00\u59cb\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column_a = df.iloc[:, 0]<\/p>\n<p>columns_ab = df.iloc[:, [0, 1]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>iloc<\/code>\u662fpandas\u63d0\u4f9b\u7684\u57fa\u4e8e\u4f4d\u7f6e\u7684\u7d22\u5f15\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u9009\u62e9\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u9009\u62e9\u5355\u5217<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5355\u5217\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165\u5217\u7d22\u5f15\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column_a = df.iloc[:, 0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aSeries\u5bf9\u8c61\uff0c\u5305\u542b\u7b2c\u4e00\u5217\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u9009\u62e9\u591a\u5217<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u591a\u5217\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165\u5305\u542b\u591a\u4e2a\u5217\u7d22\u5f15\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">columns_ab = df.iloc[:, [0, 1]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u5305\u542b\u7b2c\u4e00\u5217\u548c\u7b2c\u4e8c\u5217\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u9009\u62e9\u5217\u5e76\u8fdb\u884c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5217\u540e\uff0c\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u8ba1\u7b97\u5747\u503c\u3001\u6c42\u548c\u3001\u6392\u5e8f\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mean_a = df.iloc[:, 0].mean()<\/p>\n<p>sum_b = df.iloc[:, 1].sum()<\/p>\n<p>sorted_df = df.iloc[:, [0, 1]].sort_values(by=df.columns[0])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u9009\u62e9\u5217<\/h2>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ed3\u675f\u4f4d\u7f6e\u6765\u9009\u62e9\u8fde\u7eed\u7684\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">columns_ab = df.iloc[:, 0:2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u5305\u542b\u7b2c\u4e00\u5217\u548c\u7b2c\u4e8c\u5217\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u9009\u62e9\u5355\u5217<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5355\u5217\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6307\u5b9a\u5355\u4e2a\u5217\u7684\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ed3\u675f\u4f4d\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column_a = df.iloc[:, 0:1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u5305\u542b\u7b2c\u4e00\u5217\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u9009\u62e9\u591a\u5217<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u591a\u5217\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6307\u5b9a\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ed3\u675f\u4f4d\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">columns_bc = df.iloc[:, 1:3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u5305\u542b\u7b2c\u4e8c\u5217\u548c\u7b2c\u4e09\u5217\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u9009\u62e9\u5217\u5e76\u8fdb\u884c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5217\u540e\uff0c\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u8ba1\u7b97\u5747\u503c\u3001\u6c42\u548c\u3001\u6392\u5e8f\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mean_a = df.iloc[:, 0:1].mean()<\/p>\n<p>sum_b = df.iloc[:, 1:2].sum()<\/p>\n<p>sorted_df = df.iloc[:, 0:2].sort_values(by=df.columns[0])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u9009\u62e9\u5217<\/h2>\n<\/p>\n<p><p>\u5e03\u5c14\u7d22\u5f15\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u9009\u62e9\u7279\u5b9a\u7684\u5217\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u9009\u62e9\u5217\u540d\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">columns_with_a = df.loc[:, df.columns.str.cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns(&#39;A&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u5305\u542b\u5217\u540d\u4e2d\u5305\u542b&#39;A&#39;\u7684\u6240\u6709\u5217\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u9009\u62e9\u5355\u5217<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5355\u5217\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6307\u5b9a\u5355\u4e2a\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column_a = df.loc[:, df.columns == &#39;A&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u5305\u542b\u5217&#39;A&#39;\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u9009\u62e9\u591a\u5217<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u591a\u5217\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6307\u5b9a\u591a\u4e2a\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">columns_ab = df.loc[:, (df.columns == &#39;A&#39;) | (df.columns == &#39;B&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u5305\u542b\u5217&#39;A&#39;\u548c&#39;B&#39;\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u9009\u62e9\u5217\u5e76\u8fdb\u884c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5217\u540e\uff0c\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u8ba1\u7b97\u5747\u503c\u3001\u6c42\u548c\u3001\u6392\u5e8f\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mean_a = df.loc[:, df.columns == &#39;A&#39;].mean()<\/p>\n<p>sum_b = df.loc[:, df.columns == &#39;B&#39;].sum()<\/p>\n<p>sorted_df = df.loc[:, (df.columns == &#39;A&#39;) | (df.columns == &#39;B&#39;)].sort_values(by=&#39;A&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u9009\u53d6\u76f8\u5e94\u7684\u5217\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u4f7f\u7528pandas\u5e93\u662f\u6700\u5e38\u89c1\u548c\u65b9\u4fbf\u7684\u65b9\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7\u5217\u540d\u3001\u5217\u7d22\u5f15\u3001\u5207\u7247\u64cd\u4f5c\u548c\u5e03\u5c14\u7d22\u5f15\u9009\u62e9\u7279\u5b9a\u7684\u5217\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u8f7b\u677e\u5730\u9009\u62e9\u5217\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u9009\u4e2d\u7684\u5217\u8fdb\u884c\u5404\u79cd\u6570\u636e\u64cd\u4f5c\uff0c\u4f8b\u5982\u8ba1\u7b97\u5747\u503c\u3001\u6c42\u548c\u3001\u6392\u5e8f\u7b49\u3002pandas\u7684\u5f3a\u5927\u529f\u80fd\u548c\u7075\u6d3b\u6027\u4f7f\u5176\u6210\u4e3a\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u5fc5\u5907\u5de5\u5177\u3002\u901a\u8fc7\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\uff0c\u63d0\u5347\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u6570\u636e\u79d1\u5b66\u5bb6\u3001\u6570\u636e\u5206\u6790\u5e08\u8fd8\u662f\u5f00\u53d1\u4eba\u5458\uff0c\u5b66\u4e60\u548c\u638c\u63e1pandas\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u7406\u89e3\u548c\u5e94\u7528pandas\u5e93\u9009\u62e9\u5217\u7684\u65b9\u6cd5\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9009\u62e9\u7279\u5b9a\u7684\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u9009\u62e9\u7279\u5b9a\u7684\u5217\u3002\u53ef\u4ee5\u901a\u8fc7DataFrame\u5bf9\u8c61\u4e2d\u7684\u5217\u540d\u6765\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u5217\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>df[&#39;column_name&#39;]<\/code>\u53ef\u4ee5\u83b7\u53d6\u5355\u5217\u6570\u636e\uff0c\u800c\u4f7f\u7528<code>df[[&#39;column1&#39;, &#39;column2&#39;]]<\/code>\u5219\u53ef\u4ee5\u9009\u62e9\u591a\u5217\u3002\u786e\u4fdd\u5728\u9009\u62e9\u5217\u65f6\u5217\u540d\u4e0eDataFrame\u4e2d\u7684\u540d\u79f0\u5b8c\u5168\u4e00\u81f4\u3002<\/p>\n<p><strong>\u4f7f\u7528\u6761\u4ef6\u9009\u62e9\u5217\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u9009\u62e9\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6216\u8fc7\u6ee4\u5668\u3002\u4f8b\u5982\uff0c\u5229\u7528<code>df.loc<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9009\u62e9\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u5217\u3002\u7ed3\u5408<code>df.columns<\/code>\u5c5e\u6027\u548c\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u52a8\u6001\u9009\u62e9\u9700\u8981\u7684\u5217\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u7684\u7075\u6d3b\u6027\u9700\u6c42\u3002<\/p>\n<p><strong>\u5728\u9009\u62e9\u5217\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5f53\u9009\u62e9\u5305\u542b\u7f3a\u5931\u503c\u7684\u5217\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u4e2d\u7684<code>dropna()<\/code>\u51fd\u6570\u6765\u8fc7\u6ee4\u6389\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u3002\u4e5f\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u6765\u586b\u8865\u7f3a\u5931\u503c\u540e\u518d\u8fdb\u884c\u5217\u7684\u9009\u62e9\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u6570\u636e\u5206\u6790\u6216\u6a21\u578b\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\uff0c\u9009\u62e9\u7684\u5217\u4e0d\u4f1a\u56e0\u4e3a\u7f3a\u5931\u503c\u800c\u5f71\u54cd\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u9009\u53d6\u76f8\u5e94\u7684\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001\u901a\u8fc7\u5217\u540d\u6216\u5217\u7d22\u5f15\u8fdb\u884c\u9009\u62e9\u3001\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u53ef\u4ee5\u4f7f\u7528\u5e03 [&hellip;]","protected":false},"author":3,"featured_media":1183837,"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\/1183833"}],"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=1183833"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183833\/revisions"}],"predecessor-version":[{"id":1183840,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183833\/revisions\/1183840"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1183837"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1183833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1183833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1183833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}