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

用 pandas 自动检测银行流水中的异常交易(附完整代码)

{"type":"doc","content":[{"type":"paragraph","attrs":{"id":"3fe35bf5-306e-44c8-a6a2-21672901784e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"拿到一家企业的银行流水,几百上千行,哪笔该重点查?人工翻容易漏。本文用 pandas 按几条常见风控规则自动标出可疑交易:单笔大额、整数凑整、非工作时间/节假日交易、对手方高度集中。"}]},{"type":"paragraph","attrs":{"id":"ddf6fcd0-e92c-40a2-bf4f-1cb26a5512dd","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"一、数据假设"}]},{"type":"paragraph","attrs":{"id":"388845b5-b343-4b9b-b133-349de21e1343","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"bank.xlsx 至少包含这些列(代码中用英文列名,便于直接运行):"}]},{"type":"bulletList","attrs":{"id":"36141576-0d4a-4435-b46f-5c81cc17ccf4","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"673aeaf7-f8b3-439f-a3b6-3120b99af8f9"},"content":[{"type":"paragraph","attrs":{"id":"5f63022d-c8bd-4690-9195-1a87252b6c75","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"date 交易日期"}]}]},{"type":"listItem","attrs":{"id":"f4660fe4-bfe0-496d-be1c-2375716eb0d1"},"content":[{"type":"paragraph","attrs":{"id":"2df2b072-ca39-42bb-9958-31e22d959a98","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"amount 金额(正为收、负为付)"}]}]},{"type":"listItem","attrs":{"id":"7833f06c-ec37-4e6f-af71-dc3fe0676627"},"content":[{"type":"paragraph","attrs":{"id":"d9b795e7-172e-493b-a984-5e79a8378aa1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"counterparty 对方户名"}]}]},{"type":"listItem","attrs":{"id":"c966c428-f1a2-4b50-8385-7cce7e69cab9"},"content":[{"type":"paragraph","attrs":{"id":"a7c9ad9b-7f95-45c8-bf83-3ffe9dfe6193","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"remark 摘要"}]}]}]},{"type":"paragraph","attrs":{"id":"d1ca5f28-275e-4923-8c26-0f0e7b421dfc","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"(实战把路径和列名换成你自己的即可)"}]},{"type":"paragraph","attrs":{"id":"074bdc11-38ff-468c-a0d6-411d17545135","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"二、核心思路"}]},{"type":"orderedList","attrs":{"id":"2e5f5dad-db7e-40d1-8f3c-8fec8111b02b","start":1,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"7b85d716-f75c-4ddd-b075-8d437da0c9ba"},"content":[{"type":"paragraph","attrs":{"id":"de59d82e-994c-4cb1-a575-1c1026e49168","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"单笔金额绝对值超过阈值(如 50 万)标记大额;"}]}]},{"type":"listItem","attrs":{"id":"86b9c1e7-eed1-4618-be4e-a33a6e8af530"},"content":[{"type":"paragraph","attrs":{"id":"deec1c0d-5a68-469d-bd61-0e43ca658fce","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"金额末尾多个 0(如 100000、50000)标记凑整嫌疑;"}]}]},{"type":"listItem","attrs":{"id":"034ffd41-bf09-40bb-9839-6b215a3dc6e9"},"content":[{"type":"paragraph","attrs":{"id":"f0e078a3-1e63-45e6-bc02-78fff4291f04","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"交易日落在周末/法定节假日标记异常时间;"}]}]},{"type":"listItem","attrs":{"id":"74a6d061-0810-4595-a9f3-1ed549415945"},"content":[{"type":"paragraph","attrs":{"id":"15410e71-91a0-4ab9-be20-427d266f08bf","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"同一对手方出现次数 Top N 标记集中交易。"}]}]}]},{"type":"paragraph","attrs":{"id":"25bbd4ed-110b-46ca-8513-91676376344e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"三、完整代码(自包含,可直接跑)"}]},{"type":"codeBlock","attrs":{"id":"b77e9249-6823-4bbb-9460-9cb3f49af712","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"javascript"},"content":[{"type":"text","text":"import pandas as pd\nimport numpy as np\n\n# read your bank statement, e.g. df = pd.read_excel(\"bank.xlsx\")\ndf = pd.DataFrame({\n \"date\": [\"2026-03-01\", \"2026-03-07\", \"2026-03-15\", \"2026-03-22\", \"2026-03-22\"],\n \"amount\": [800000, -100000, -5200, 50000, 50000],\n \"counterparty\": [\"A Co\", \"B Co\", \"C Shop\", \"D Co\", \"D Co\"],\n \"remark\": [\"loan\", \"transfer\", \"meal\", \"pay\", \"pay\"],\n})\ndf[\"date\"] = pd.to_datetime(df[\"date\"])\n\nTHRESH = 500000\nflags = []\n\n# rule 1: large amount\nflags.append(df[\"amount\"].abs() > THRESH)\n\n# rule 2: round-number (ends with many zeros)\ndef is_round(x):\n return x != 0 and (abs(x) % 10000 == 0)\nflags.append(df[\"amount\"].apply(is_round))\n\n# rule 3: weekend\nflags.append(df[\"date\"].dt.dayofweek >= 5)\n\n# rule 4: concentrated counterparty (top by frequency)\ntop_cp = df[\"counterparty\"].value_counts().head(3).index\nflags.append(df[\"counterparty\"].isin(top_cp))\n\ndf[\"flag\"] = np.any(flags, axis=0)\nprint(df[df[\"flag\"]][[\"date\", \"amount\", \"counterparty\", \"remark\"]])\n"}]},{"type":"paragraph","attrs":{"id":"a45ea093-7735-46ac-a727-b8162c04d8e1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"四、几个实战坑"}]},{"type":"bulletList","attrs":{"id":"a2334c30-a7e8-4ad7-b370-c95e2345c62e","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"295fe942-4da8-4e2b-84ca-254c5d655f70"},"content":[{"type":"paragraph","attrs":{"id":"36b56c46-be53-416e-ac73-f38f924d1cdb","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"阈值按企业规模定,小公司 50 万算大,集团可能 500 万才算;"}]}]},{"type":"listItem","attrs":{"id":"d9b5fda1-b83d-476b-afde-c2bed1da5425"},"content":[{"type":"paragraph","attrs":{"id":"6726a96a-e6eb-4b39-b2cd-1ad9f6c8a210","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"节假日要接日历表,不能只判周末;"}]}]},{"type":"listItem","attrs":{"id":"cde7f13d-0408-4493-8b4a-8586951afac9"},"content":[{"type":"paragraph","attrs":{"id":"962e9388-7788-48a3-8734-2fff1e69c25b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"凑整规则对\"工资发放\"\"房租\"等合法整数交易会误报,需白名单;"}]}]},{"type":"listItem","attrs":{"id":"905af575-30dd-4f19-b885-54bf452aa43a"},"content":[{"type":"paragraph","attrs":{"id":"c0b5db88-bc6e-438f-aef8-6152053c7961","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"\"对手方集中\"结合金额看,小额高频和单笔大额意义不同;"}]}]},{"type":"listItem","attrs":{"id":"ba6efc48-1739-4bd2-b329-304760ab88ae"},"content":[{"type":"paragraph","attrs":{"id":"54891dca-1486-458c-b568-1ee53ca689d1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"读 csv 注意指定编码为 gbk(Windows 中文环境),xlsx 一般无此问题。"}]}]}]},{"type":"paragraph","attrs":{"id":"9e29ee15-b099-43b9-8e93-5b2d30b4c5e1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"五、边界"}]},{"type":"paragraph","attrs":{"id":"26abaa85-e596-45fb-9702-6c3c3676cea2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"脚本只负责把可疑交易列出来,是否异常、怎么定性,注册会计师判。客户数据本地跑,别贴通用云端大模型。"}]},{"type":"paragraph","attrs":{"id":"980a44e7-3963-4289-8229-dd27925cf01c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"我是会计师事务所的一名注册会计师,做过财报审计、经济责任审计、专项审计、尽职调查、内控评价各类项目,从手写底稿到 AI 辅助都经历过。如果你也在财务 / 审计岗位想提效,欢迎关注、交流实战。"}]},{"type":"paragraph","attrs":{"id":"9fa39346-21c5-42bf-ac27-0f5fa17f93df","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"感谢你看到这里。如果这段代码对你有用,欢迎点赞 + 收藏 + 关注——我是会计师事务所的一名注册会计师,会继续分享审计提效与 Python 实战,咱们下篇见。"}]}]}

下一篇
举报
领券