{"id":12364,"date":"2026-06-10T04:00:02","date_gmt":"2026-06-09T20:00:02","guid":{"rendered":"https:\/\/ab123.xyz\/?p=12364"},"modified":"2026-06-10T04:00:02","modified_gmt":"2026-06-09T20:00:02","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%ef%bc%9a%e6%8f%90%e5%8d%87%e6%95%b0%e6%8d%ae%e5%a4%84%e7%90%86%e6%95%88%e7%8e%87%e7%9a%84","status":"publish","type":"post","link":"https:\/\/ab123.xyz\/?p=12364","title":{"rendered":"Python \u6570\u636e\u5206\u6790 pandas \u6027\u80fd\u8c03\u4f18\u6280\u5de7\uff1a\u63d0\u5347\u6570\u636e\u5904\u7406\u6548\u7387\u7684\u5fc5\u5907\u6307\u5357"},"content":{"rendered":"<p>\u5728 Python \u6570\u636e\u5206\u6790\u9886\u57df\uff0cpandas \u662f\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u7684\u6838\u5fc3\u5de5\u5177\u3002\u7136\u800c\uff0c\u5f53\u6570\u636e\u91cf\u8fbe\u5230\u767e\u4e07\u7ea7\u751a\u81f3\u4ebf\u7ea7\u65f6\uff0c\u6027\u80fd\u74f6\u9888\u53ef\u80fd\u4e25\u91cd\u5f71\u54cd\u5f00\u53d1\u6548\u7387\u3002\u672c\u6587\u5c06\u6df1\u5165\u4ecb\u7ecd\u4e00\u7cfb\u5217\u7ecf\u8fc7\u9a8c\u8bc1\u7684 pandas \u6027\u80fd\u8c03\u4f18\u6280\u5de7\uff0c\u5e2e\u52a9\u60a8\u663e\u8457\u63d0\u5347\u6570\u636e\u5904\u7406\u901f\u5ea6\u3002\u5982\u9700\u83b7\u53d6\u5b8c\u6574\u6587\u6863\u4e0e\u6700\u65b0\u7248\u672c\uff0c\u8bf7\u8bbf\u95ee <a href=\"https:\/\/pandas.pydata.org\" target=\"_blank\">\u5b98\u65b9\u7f51\u7ad9<\/a>\u3002<\/p>\n<h2>1. \u5411\u91cf\u5316\u64cd\u4f5c\u4e0e\u907f\u514d\u663e\u5f0f\u5faa\u73af<\/h2>\n<p>pandas \u5e95\u5c42\u57fa\u4e8e NumPy \u7684\u5411\u91cf\u5316\u8fd0\u7b97\uff0c\u8fd9\u662f\u5176\u9ad8\u6027\u80fd\u7684\u5173\u952e\u3002\u5e94\u5c3d\u91cf\u907f\u514d\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u6216 <code>apply<\/code> \u65b9\u6cd5\u9010\u884c\u5904\u7406\u6570\u636e\uff0c\u800c\u662f\u4f18\u5148\u4f7f\u7528\u5185\u7f6e\u7684\u5411\u91cf\u5316\u51fd\u6570\uff08\u5982 <code>df['col'].mean()<\/code>\u3001<code>df.eval()<\/code>\u3001<code>df.query()<\/code>\uff09\u3002\u4f8b\u5982\uff0c\u5bf9\u4e24\u5217\u6c42\u548c\u65f6\uff0c\u76f4\u63a5\u4f7f\u7528 <code>df['sum'] = df['a'] + df['b']<\/code> \u6bd4 <code>df.apply(lambda row: row['a']+row['b'], axis=1)<\/code> \u5feb\u6570\u5341\u500d\u3002<\/p>\n<h3>1.1 \u5229\u7528 NumPy \u901a\u7528\u51fd\u6570<\/h3>\n<p>\u5bf9\u4e8e\u590d\u6742\u6570\u5b66\u8fd0\u7b97\uff0c\u53ef\u501f\u52a9 <code>np.where<\/code>\u3001<code>np.select<\/code> \u66ff\u4ee3\u6761\u4ef6\u5faa\u73af\uff0c\u51cf\u5c11 Python \u5c42\u9762\u7684\u5f00\u9500\u3002<\/p>\n<h2>2. \u6570\u636e\u7c7b\u578b\u4f18\u5316\u4e0e\u5185\u5b58\u7ba1\u7406<\/h2>\n<p>pandas \u9ed8\u8ba4\u4f7f\u7528 64 \u4f4d\u6570\u636e\u7c7b\u578b\uff0c\u5e38\u5bfc\u81f4\u5185\u5b58\u6d6a\u8d39\u3002\u901a\u8fc7 <code>df.info()<\/code> \u68c0\u67e5\u5404\u5217\u7c7b\u578b\uff0c\u5c06 <code>float64<\/code> \u8f6c\u4e3a <code>float32<\/code>\uff0c<code>int64<\/code> \u8f6c\u4e3a <code>int32<\/code> \u6216 <code>int8<\/code>\uff0c\u53ef\u51cf\u5c11\u4e00\u534a\u5185\u5b58\u5360\u7528\u3002\u5bf9\u4e8e\u7c7b\u522b\u578b\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 <code>category<\/code> \u7c7b\u578b\u80fd\u5927\u5e45\u964d\u4f4e\u5185\u5b58\u5e76\u63d0\u901f\u5206\u7ec4\u8fd0\u7b97\u3002<\/p>\n<h3>2.1 \u4f7f\u7528\u7a00\u758f\u6570\u636e\u7ed3\u6784<\/h3>\n<p>\u5bf9\u4e8e\u542b\u5927\u91cf\u7a7a\u503c\u6216\u91cd\u590d\u503c\u7684\u6570\u636e\u96c6\uff0c\u53ef\u542f\u7528 <code>pd.arrays.SparseArray<\/code> \u6216 <code>pd.DataFrame.sparse<\/code> \u7cfb\u5217\uff0c\u4ec5\u5b58\u50a8\u975e\u9ed8\u8ba4\u503c\uff0c\u8282\u7701\u5185\u5b58\u3002<\/p>\n<h2>3. \u9ad8\u6548\u8bfb\u53d6\u4e0e\u5206\u5757\u5904\u7406<\/h2>\n<p>\u8bfb\u53d6\u5927\u578b CSV \u6587\u4ef6\u65f6\uff0c\u901a\u8fc7 <code>pd.read_csv(..., dtype=..., engine='c')<\/code> \u6307\u5b9a\u5217\u7c7b\u578b\u548c C \u5f15\u64ce\u53ef\u52a0\u5feb\u89e3\u6790\u901f\u5ea6\u3002\u82e5\u5185\u5b58\u4e0d\u8db3\uff0c\u4f7f\u7528 <code>chunksize<\/code> \u53c2\u6570\u5206\u5757\u8bfb\u53d6\uff0c\u9010\u5757\u5904\u7406\u540e\u518d\u805a\u5408\u3002\u6b64\u5916\uff0c\u5c06\u6570\u636e\u5b58\u50a8\u4e3a Parquet \u6216 HDF5 \u683c\u5f0f\uff0c\u8bfb\u5199\u901f\u5ea6\u8fdc\u8d85 CSV\u3002<\/p>\n<h3>3.1 \u7d22\u5f15\u4f18\u5316\u4e0e\u6392\u5e8f<\/h3>\n<p>\u4e3a\u7ecf\u5e38\u67e5\u8be2\u6216\u5206\u7ec4\u7684\u5217\u8bbe\u7f6e\u7d22\u5f15\uff08<code>df.set_index()<\/code>\uff09\uff0c\u53ef\u52a0\u901f\u68c0\u7d22\u3002\u5bf9\u4e8e\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u4f7f\u7528 <code>DatetimeIndex<\/code> \u5e76\u8c03\u7528 <code>sort_index()<\/code> \u786e\u4fdd\u6709\u5e8f\uff0c\u63d0\u5347\u6ed1\u52a8\u7a97\u53e3\u7b49\u64cd\u4f5c\u7684\u6548\u7387\u3002<\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u5728\u4e0d\u66f4\u6362\u786c\u4ef6\u7684\u60c5\u51b5\u4e0b\u5c06 pandas \u6570\u636e\u5904\u7406\u901f\u5ea6\u63d0\u5347\u6570\u500d\u3002\u5efa\u8bae\u5b9a\u671f\u914d\u5408 <code>%timeit<\/code> \u6216 <code>cProfile<\/code> \u5206\u6790\u74f6\u9888\uff0c\u6301\u7eed\u4f18\u5316\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u6570\u636e\u5206\u6790\u9886\u57df\uff0cpandas \u662f\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u7684\u6838\u5fc3\u5de5\u5177\u3002\u7136\u800c\uff0c\u5f53\u6570\u636e\u91cf\u8fbe\u5230\u767e\u4e07\u7ea7\u751a\u81f3\u4ebf\u7ea7\u65f6\uff0c [&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,15024,15025,15023],"class_list":["post-12364","post","type-post","status-publish","format-standard","hentry","category-4","tag-pandas","tag-python","tag-15024","tag-15025","tag-15023"],"_links":{"self":[{"href":"https:\/\/ab123.xyz\/index.php?rest_route=\/wp\/v2\/posts\/12364","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=12364"}],"version-history":[{"count":1,"href":"https:\/\/ab123.xyz\/index.php?rest_route=\/wp\/v2\/posts\/12364\/revisions"}],"predecessor-version":[{"id":12365,"href":"https:\/\/ab123.xyz\/index.php?rest_route=\/wp\/v2\/posts\/12364\/revisions\/12365"}],"wp:attachment":[{"href":"https:\/\/ab123.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ab123.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ab123.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}