首页
学习
活动
专区
圈层
工具
发布

用 pandas 快速筛查异常会计分录(附完整代码)

{"type":"doc","content":[{"type":"paragraph","attrs":{"id":"375e4753-8706-4f29-a997-3828a1710195","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"做账务复核时,最怕三类低级错误:同一张凭证借贷不平、摘要空白、凭证号重复。手工翻几百行容易漏,本文用 pandas 一键扫出来。"}]},{"type":"paragraph","attrs":{"id":"cdd31ead-6581-43a9-8211-a911c34e7711","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"一、数据假设"}]},{"type":"paragraph","attrs":{"id":"e5be0a9d-3778-4ba3-9d1f-c01db3402dbf","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"vouchers.xlsx 至少包含这些列(代码中用英文列名,便于直接运行):"}]},{"type":"bulletList","attrs":{"id":"c054f4db-37ff-4190-8535-33f8dd8bfb71","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"4cc18b59-ad5a-455e-9d67-ccd9b2770710"},"content":[{"type":"paragraph","attrs":{"id":"75798741-b4b3-45bd-8cd9-a1db3d6859b1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"voucher 凭证号"}]}]},{"type":"listItem","attrs":{"id":"bdee378b-9cec-47a7-92d5-4872b86813de"},"content":[{"type":"paragraph","attrs":{"id":"a1528567-8f05-4571-809c-599e07e43ec6","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"date 日期"}]}]},{"type":"listItem","attrs":{"id":"7b9ed0d0-3b3a-4005-b5d0-ba21f4159082"},"content":[{"type":"paragraph","attrs":{"id":"4cf7c685-192a-4fcf-bdc0-c82017c7a5cd","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"desc 摘要"}]}]},{"type":"listItem","attrs":{"id":"d295decd-954b-4e5e-bfa8-20727b3f951e"},"content":[{"type":"paragraph","attrs":{"id":"85736f2a-47b9-4bce-be1e-f6a02e89ada7","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"account 科目"}]}]},{"type":"listItem","attrs":{"id":"59047312-5284-42c1-afcb-9fd7587119e8"},"content":[{"type":"paragraph","attrs":{"id":"7f121702-eda9-4429-88db-0bc768dfdedd","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"debit 借方"}]}]},{"type":"listItem","attrs":{"id":"8c9f7784-d2c6-45dc-bbcd-069c0f428516"},"content":[{"type":"paragraph","attrs":{"id":"e2c0f880-a095-49a2-a2e9-0bcea22f9419","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"credit 贷方"}]}]}]},{"type":"paragraph","attrs":{"id":"66fdd94b-1056-4f2c-88a4-12cbf1a52e08","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"(实战把路径和列名换成你自己的即可)"}]},{"type":"paragraph","attrs":{"id":"2cc97aa0-3d3b-413f-a03e-5c9b2195f562","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"二、核心思路"}]},{"type":"orderedList","attrs":{"id":"b651c518-bdd7-455e-acd2-acd7604f91bb","start":1,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"7964b9b2-4c58-4fff-90ef-43cc660d1512"},"content":[{"type":"paragraph","attrs":{"id":"a6b01dac-e60d-4ea2-aa75-973da5080ecc","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"按凭证号分组,求每组借贷合计,差额不为零即不平;"}]}]},{"type":"listItem","attrs":{"id":"e0c887c1-429c-4106-9c82-ee2c219f447b"},"content":[{"type":"paragraph","attrs":{"id":"773f43ff-11ba-4cbe-afde-a93b6a2f8980","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"摘要为空(或纯空格)的行单独拎出;"}]}]},{"type":"listItem","attrs":{"id":"0057bc76-a176-481d-b293-71bb65f000e2"},"content":[{"type":"paragraph","attrs":{"id":"8d1b223c-135a-4e27-a5ac-f04a5f8fd8ab","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"凭证号出现次数大于 1 即重复。"}]}]}]},{"type":"paragraph","attrs":{"id":"85fc7901-117b-4474-93a9-6e8bb12232c0","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"三、完整代码(自包含,可直接跑)"}]},{"type":"codeBlock","attrs":{"id":"f316703c-5efd-4456-b9f9-d0243fd2cac9","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"javascript"},"content":[{"type":"text","text":"import pandas as pd\n\n# read your voucher excel, e.g. df = pd.read_excel(\"vouchers.xlsx\")\ndf = pd.DataFrame({\n \"voucher\": [\"J-001\", \"J-001\", \"J-002\", \"J-003\", \"J-004\"],\n \"date\": [\"2026-03-01\", \"2026-03-01\", \"2026-03-02\", \"2026-03-03\", \"2026-03-04\"],\n \"desc\": [\"sale\", \"sale\", \"\", \"salary\", \"rent\"],\n \"account\": [\"AR\", \"Cash\", \"Cash\", \"Expense\", \"Expense\"],\n \"debit\": [0, 8000, 5000, 2000, 3000],\n \"credit\": [8000, 0, 0, 0, 0],\n})\n\n# 1. unbalanced voucher: debit total != credit total\nbal = df.groupby(\"voucher\")[[\"debit\", \"credit\"]].sum().reset_index()\nbal[\"diff\"] = (bal[\"debit\"] - bal[\"credit\"]).abs().round(2)\nunbalanced = bal[bal[\"diff\"] > 0.01]\n\n# 2. empty description\nempty_desc = df[df[\"desc\"].astype(str).str.strip() == \"\"]\n\n# 3. duplicate voucher numbers\ndup_count = df[\"voucher\"].value_counts()\ndup_count = dup_count[dup_count > 1]\n\nprint(\"unbalanced vouchers:\")\nprint(unbalanced)\nprint(\"rows with empty description:\")\nprint(empty_desc[[\"voucher\", \"account\"]])\nprint(\"duplicate voucher counts:\")\nprint(dup_count)\n"}]},{"type":"paragraph","attrs":{"id":"26c7dcf9-53bc-4c1c-b72d-beee00c47f81","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"四、几个实战坑"}]},{"type":"bulletList","attrs":{"id":"4351de21-77e8-45d8-8d03-e9e5bbd8ff71","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"03e32306-b8b8-4d8b-be8c-2699b9165723"},"content":[{"type":"paragraph","attrs":{"id":"841b381e-fae6-4ea2-b0cd-407111973aad","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"浮点精度:金额比对先 .round(2) 再判零,避免 0.0001 误报;"}]}]},{"type":"listItem","attrs":{"id":"a3f8c1cc-ec3e-4161-8ea2-e519ac542484"},"content":[{"type":"paragraph","attrs":{"id":"a8ed2c35-66bb-49d6-a23d-eb7ab1509f18","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"一张凭证一借多贷很正常,要按\"凭证号\"聚合后再比借贷,别按单行比;"}]}]},{"type":"listItem","attrs":{"id":"c2acd86e-9b21-4714-8e1b-53dbc79cca57"},"content":[{"type":"paragraph","attrs":{"id":"43fdf9e7-4e7c-4e85-9d2d-a09046ae3a11","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"摘要为空也可能合法(如结转凭证),预警后人工确认;"}]}]},{"type":"listItem","attrs":{"id":"db8c66d6-f000-4737-ac0b-7c168f83271a"},"content":[{"type":"paragraph","attrs":{"id":"fad27c8d-4026-4921-9430-676db2f64975","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"读 csv 注意指定编码为 gbk(Windows 中文环境),xlsx 一般无此问题。"}]}]}]},{"type":"paragraph","attrs":{"id":"a1bf6530-9f6a-44dc-941a-b27f8397117c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"五、边界"}]},{"type":"paragraph","attrs":{"id":"36f1729d-488f-43da-b3bc-1f1ce8547957","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"脚本只负责把可疑项列出来,是否错报、怎么调,注册会计师判断签字。客户数据本地跑,别贴通用云端大模型。"}]},{"type":"paragraph","attrs":{"id":"8fb33749-ff64-4e5b-8dd1-eeacb4bdac8c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"我是会计师事务所的一名注册会计师,做过财报审计、经济责任审计、专项审计、尽职调查、内控评价各类项目,从手写底稿到 AI 辅助都经历过。如果你也在财务 / 审计岗位想提效,欢迎关注、交流实战。"}]},{"type":"paragraph","attrs":{"id":"6d579d71-c8ba-4479-814d-2e2f50d6338a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"感谢你看到这里。如果这段代码对你有用,欢迎点赞 + 收藏 + 关注——我是会计师事务所的一名注册会计师,会继续分享审计提效与 Python 实战,咱们下篇见。"}]},{"type":"paragraph","attrs":{"id":"b1da8465-616d-4b0d-9d32-3a267e366ba7","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}}]}

举报
领券