{"id":12662,"date":"2026-06-10T04:18:10","date_gmt":"2026-06-09T20:18:10","guid":{"rendered":"https:\/\/ab123.xyz\/?p=12662"},"modified":"2026-06-10T04:18:10","modified_gmt":"2026-06-09T20:18:10","slug":"python-%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90-pandas-%e6%80%a7%e8%83%bd%e8%b0%83%e4%bc%98%e6%8a%80%e5%b7%a7%e6%9d%83%e5%a8%81%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/ab123.xyz\/?p=12662","title":{"rendered":"Python \u6570\u636e\u5206\u6790 pandas \u6027\u80fd\u8c03\u4f18\u6280\u5de7\u6743\u5a01\u6307\u5357"},"content":{"rendered":"<p>\u5728 Python \u6570\u636e\u5206\u6790\u9886\u57df\uff0cpandas \u51e0\u4e4e\u6210\u4e3a\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u7684\u6807\u51c6\u5de5\u5177\u3002\u7136\u800c\uff0c\u5f53\u6570\u636e\u91cf\u8fbe\u5230\u6570\u767e\u4e07\u884c\u751a\u81f3\u66f4\u591a\u65f6\uff0cpandas \u7684\u9ed8\u8ba4\u64cd\u4f5c\u53ef\u80fd\u4f1a\u53d8\u5f97\u6781\u5176\u7f13\u6162\u3002\u638c\u63e1\u6027\u80fd\u8c03\u4f18\u6280\u5de7\uff0c\u4e0d\u4ec5\u80fd\u663e\u8457\u63d0\u5347\u6570\u636e\u5904\u7406\u6548\u7387\uff0c\u8fd8\u80fd\u5728\u7ade\u4e89\u6fc0\u70c8\u7684\u6570\u636e\u5206\u6790\u5de5\u4f5c\u4e2d\u5360\u636e\u4f18\u52bf\u3002\u672c\u6587\u57fa\u4e8e\u5b98\u65b9\u6587\u6863\u4e0e\u793e\u533a\u6700\u4f73\u5b9e\u8df5\uff0c\u7cfb\u7edf\u4ecb\u7ecd\u51e0\u79cd\u6838\u5fc3\u4f18\u5316\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u5145\u5206\u53d1\u6325 pandas \u7684\u6f5c\u529b\u3002\u9996\u5148\uff0c\u8bbf\u95ee <a href=\"https:\/\/pandas.pydata.org\" target=\"_blank\">\u5b98\u65b9\u7f51\u7ad9<\/a> \u83b7\u53d6\u6700\u65b0\u7248\u672c\u4e0e\u6587\u6863\uff0c\u8fd9\u662f\u6240\u6709\u8c03\u4f18\u7684\u57fa\u7840\u3002<\/p>\n<h2>\u6570\u636e\u7c7b\u578b\u4f18\u5316\uff1a\u4ece\u6839\u672c\u63d0\u901f<\/h2>\n<p>pandas \u9ed8\u8ba4\u4f7f\u7528 Python \u5bf9\u8c61\u7c7b\u578b\u5b58\u50a8\u6570\u636e\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u548c\u8fd0\u7b97\u7f13\u6162\u3002\u901a\u8fc7\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u66f4\u7d27\u51d1\u7684 NumPy \u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u5927\u5e45\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u5e76\u63d0\u5347\u8fd0\u7b97\u901f\u5ea6\u3002\u4f8b\u5982\uff0c\u5bf9\u6574\u6570\u5217\u4f7f\u7528 <code>int32<\/code> \u6216 <code>int8<\/code> \u800c\u975e <code>int64<\/code>\uff0c\u5bf9\u6d6e\u70b9\u6570\u5217\u4f7f\u7528 <code>float32<\/code>\uff0c\u5bf9\u6587\u672c\u5217\u5219\u4f7f\u7528 <code>category<\/code> \u7c7b\u578b\u3002\u5177\u4f53\u64cd\u4f5c\u53ef\u901a\u8fc7 <code>astype()<\/code> \u65b9\u6cd5\u5b9e\u73b0\u3002\u6b64\u5916\uff0c\u4f7f\u7528 <code>pd.read_csv()<\/code> \u65f6\u6307\u5b9a <code>dtype<\/code> \u53c2\u6570\uff0c\u80fd\u5728\u52a0\u8f7d\u9636\u6bb5\u76f4\u63a5\u5b8c\u6210\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<h3>\u5e38\u89c1\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u793a\u4f8b<\/h3>\n<ul>\n<li>\u6574\u6570\u5217\uff1a<code>df['col'] = df['col'].astype('int32')<\/code><\/li>\n<li>\u7c7b\u522b\u5217\uff1a<code>df['category'] = df['category'].astype('category')<\/code><\/li>\n<li>\u65f6\u95f4\u5217\uff1a\u4f7f\u7528 <code>pd.to_datetime()<\/code> \u5e76\u6307\u5b9a\u683c\u5f0f\u53ef\u52a0\u901f\u89e3\u6790<\/li>\n<\/ul>\n<h2>\u5411\u91cf\u5316\u64cd\u4f5c\u4e0e\u907f\u514d\u5faa\u73af<\/h2>\n<p>pandas \u6700\u5f3a\u5927\u7684\u7279\u6027\u4e4b\u4e00\u5c31\u662f\u5411\u91cf\u5316\u64cd\u4f5c\u2014\u2014\u6574\u4e2a\u5217\u6216 DataFrame \u4e0a\u7684\u8fd0\u7b97\u7531\u5e95\u5c42 C \u8bed\u8a00\u5b9e\u73b0\uff0c\u901f\u5ea6\u8fdc\u8d85 Python \u5faa\u73af\u3002\u4efb\u4f55\u9700\u8981\u6309\u884c\u6216\u5143\u7d20\u5904\u7406\u7684\u903b\u8f91\uff0c\u90fd\u5e94\u4f18\u5148\u8003\u8651\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\uff08\u5982 <code>map()<\/code>\u3001<code>apply()<\/code>\u3001<code>agg()<\/code> \u7b49\uff09\u3002\u4f46\u8981\u6ce8\u610f\uff0c<code>apply()<\/code> \u672c\u8d28\u4e0a\u4ecd\u662f\u9010\u884c\u5faa\u73af\uff0c\u82e5\u903b\u8f91\u590d\u6742\u53ef\u6539\u7528 <code>numpy<\/code> \u51fd\u6570\u6216<code>pandas.eval()<\/code>\u3001<code>query()<\/code> \u7b49\u8868\u8fbe\u5f0f\u5f15\u64ce\u52a0\u901f\u3002<\/p>\n<h3>\u5e94\u7528\u573a\u666f\u5bf9\u6bd4<\/h3>\n<ul>\n<li>\u7b80\u5355\u6570\u5b66\u8fd0\u7b97\uff1a\u76f4\u63a5\u4f7f\u7528 DataFrame \u7b97\u672f\u8fd0\u7b97\u7b26\uff08\u5411\u91cf\u5316\uff09<\/li>\n<li>\u6761\u4ef6\u7b5b\u9009\uff1a<code>df.query('column &gt; value')<\/code> \u6bd4\u5e03\u5c14\u7d22\u5f15\u66f4\u5feb<\/li>\n<li>\u805a\u5408\u8ba1\u7b97\uff1a<code>groupby<\/code> \u7ed3\u5408 <code>agg<\/code> \u4f7f\u7528\u805a\u5408\u51fd\u6570\u5411\u91cf\u5316<\/li>\n<\/ul>\n<h2>\u7d22\u5f15\u4e0e\u5206\u5757\u8bfb\u53d6\u7b56\u7565<\/h2>\n<p>\u5bf9\u4e8e\u8d85\u5927\u6570\u636e\u96c6\uff0c\u4e00\u6b21\u6027\u52a0\u8f7d\u5230\u5185\u5b58\u5f80\u5f80\u5bfc\u81f4\u5d29\u6e83\u3002\u53ef\u4ee5\u5229\u7528 <code>chunksize<\/code> \u53c2\u6570\u5206\u5757\u8bfb\u53d6 CSV \u6587\u4ef6\uff0c\u6bcf\u6b21\u5904\u7406\u4e00\u5c0f\u90e8\u5206\u5e76\u9010\u6b65\u6c47\u805a\u7ed3\u679c\u3002\u540c\u65f6\uff0c\u5408\u7406\u8bbe\u7f6e\u7d22\u5f15\uff08\u5982\u4f7f\u7528 <code>set_index()<\/code> \u5c06\u65f6\u95f4\u5217\u6216 ID \u5217\u8bbe\u4e3a\u7d22\u5f15\uff09\u80fd\u663e\u8457\u52a0\u5feb\u57fa\u4e8e\u7d22\u5f15\u7684\u67e5\u8be2\u901f\u5ea6\u3002\u6b64\u5916\uff0c<code>inplace=True<\/code> \u64cd\u4f5c\u867d\u7136\u65b9\u4fbf\uff0c\u4f46\u53ef\u80fd\u5f15\u8d77\u94fe\u5f0f\u526f\u672c\u95ee\u9898\uff0c\u5efa\u8bae\u5c3d\u91cf\u4f7f\u7528\u8d4b\u503c\u5f0f\u5199\u6cd5\u3002<\/p>\n<h3>\u5206\u5757\u8bfb\u53d6\u793a\u4f8b<\/h3>\n<ul>\n<li><code>for chunk in pd.read_csv('large.csv', chunksize=100000):<\/code> \u9010\u5757\u5904\u7406<\/li>\n<li>\u5229\u7528 <code>dask.dataframe<\/code> \u6216 <code>modin<\/code> \u7b49\u5e76\u884c\u6846\u67b6\u8fdb\u4e00\u6b65\u6269\u5c55 pandas \u80fd\u529b<\/li>\n<\/ul>\n<p>\u638c\u63e1\u4ee5\u4e0a\u6280\u5de7\u540e\uff0c\u4f60\u53ef\u4ee5\u5c06 pandas \u5904\u7406\u901f\u5ea6\u63d0\u5347\u6570\u5341\u500d\u3002\u4e3a\u4e86\u6301\u7eed\u83b7\u53d6\u6700\u65b0\u4f18\u5316\u65b9\u6cd5\uff0c\u52a1\u5fc5\u6536\u85cf <a href=\"https:\/\/pandas.pydata.org\" target=\"_blank\">\u5b98\u65b9\u7f51\u7ad9<\/a>\u3002\u540c\u65f6\uff0cpandas \u793e\u533a\u5728 GitHub \u4e0a\u5b9a\u671f\u53d1\u5e03\u6027\u80fd\u6539\u8fdb\uff0c\u5efa\u8bae\u5173\u6ce8\u5176\u53d1\u5e03\u65e5\u5fd7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u6570\u636e\u5206\u6790\u9886\u57df\uff0cpandas \u51e0\u4e4e\u6210\u4e3a\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u7684\u6807\u51c6\u5de5\u5177\u3002\u7136\u800c\uff0c\u5f53\u6570\u636e\u91cf\u8fbe\u5230\u6570\u767e\u4e07\u884c\u751a\u81f3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[15021,15022,15408,15409,15407],"class_list":["post-12662","post","type-post","status-publish","format-standard","hentry","category-4","tag-pandas","tag-python","tag-15408","tag-15409","tag-15407"],"_links":{"self":[{"href":"https:\/\/ab123.xyz\/index.php?rest_route=\/wp\/v2\/posts\/12662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ab123.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ab123.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ab123.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ab123.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12662"}],"version-history":[{"count":1,"href":"https:\/\/ab123.xyz\/index.php?rest_route=\/wp\/v2\/posts\/12662\/revisions"}],"predecessor-version":[{"id":12663,"href":"https:\/\/ab123.xyz\/index.php?rest_route=\/wp\/v2\/posts\/12662\/revisions\/12663"}],"wp:attachment":[{"href":"https:\/\/ab123.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ab123.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ab123.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}