{"id":1082190,"date":"2025-01-08T12:46:35","date_gmt":"2025-01-08T04:46:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1082190.html"},"modified":"2025-01-08T12:46:37","modified_gmt":"2025-01-08T04:46:37","slug":"python%e5%a6%82%e4%bd%95%e5%8c%b9%e9%85%8d%e4%b8%a4%e5%88%97%e6%95%b0%e6%8d%ae%e5%ba%93-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1082190.html","title":{"rendered":"python\u5982\u4f55\u5339\u914d\u4e24\u5217\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183842\/a8057c8a-5cc2-48b8-85db-638b9cac7384.webp\" alt=\"python\u5982\u4f55\u5339\u914d\u4e24\u5217\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5339\u914d\u4e24\u5217\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001SQL\u67e5\u8be2\u3001\u6216ORM\uff08\u5982SQLAlchemy\uff09\u3002\u4f7f\u7528pandas\u5e93\u6700\u4e3a\u7b80\u5355\u4e14\u76f4\u89c2\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u5e93\u5339\u914d\u4e24\u5217\u6570\u636e\u5e93\uff0c\u9996\u5148\u9700\u8981\u5c06\u6570\u636e\u5e93\u8868\u52a0\u8f7d\u5230DataFrame\u4e2d\uff0c\u7136\u540e\u4f7f\u7528merge\u51fd\u6570\u8fdb\u884c\u5339\u914d\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528inner join\u3001left join\u3001right join\u7b49\u65b9\u5f0f<\/strong>\u6765\u5b9e\u73b0\u6570\u636e\u7684\u5339\u914d\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u4e24\u5217\u6570\u636e\u5e93\u7684\u5339\u914d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u5339\u914d<\/h3>\n<\/p>\n<ol>\n<li><strong>\u52a0\u8f7d\u6570\u636e\u5230DataFrame<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u6570\u636e\u5339\u914d\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5c06\u6570\u636e\u52a0\u8f7d\u5230pandas\u7684DataFrame\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u7684read_sql\u51fd\u6570\u6765\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>df1 = pd.read_sql(&#39;SELECT * FROM table1&#39;, conn)<\/p>\n<p>df2 = pd.read_sql(&#39;SELECT * FROM table2&#39;, conn)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5339\u914d\u4e24\u5217\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528pandas\u7684merge\u51fd\u6570\u6765\u5339\u914d\u4e24\u5217\u6570\u636e\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u4e0d\u540c\u7684\u5339\u914d\u65b9\u5f0f\uff0c\u5982inner join\u3001left join\u3001right join\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528inner join\u5339\u914d\u4e24\u5217\u6570\u636e<\/p>\n<p>result = pd.merge(df1, df2, on=&#39;column_name&#39;, how=&#39;inner&#39;)<\/p>\n<h2><strong>\u6253\u5370\u5339\u914d\u7ed3\u679c<\/strong><\/h2>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>on<\/code>\u53c2\u6570\u6307\u5b9a\u8981\u5339\u914d\u7684\u5217\u540d\uff0c<code>how<\/code>\u53c2\u6570\u6307\u5b9a\u5339\u914d\u65b9\u5f0f\uff0c\u53ef\u4ee5\u9009\u62e9&#39;inner&#39;\u3001&#39;left&#39;\u3001&#39;right&#39;\u6216&#39;outer&#39;\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528SQL\u67e5\u8be2\u8fdb\u884c\u5339\u914d<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528pandas\u5e93\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528SQL\u67e5\u8be2\u6765\u5339\u914d\u4e24\u5217\u6570\u636e\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528SQLite\u6570\u636e\u5e93\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\u9700\u8981\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6267\u884cSQL\u67e5\u8be2<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528SQL\u67e5\u8be2\u6765\u5339\u914d\u4e24\u5217\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528JOIN\u64cd\u4f5c\u6765\u5b9e\u73b0\u5339\u914d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6267\u884cSQL\u67e5\u8be2<\/p>\n<p>cursor.execute(&#39;&#39;&#39;<\/p>\n<p>SELECT table1.*, table2.*<\/p>\n<p>FROM table1<\/p>\n<p>JOIN table2 ON table1.column_name = table2.column_name<\/p>\n<p>&#39;&#39;&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>result = cursor.fetchall()<\/p>\n<h2><strong>\u6253\u5370\u5339\u914d\u7ed3\u679c<\/strong><\/h2>\n<p>for row in result:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/li>\n<\/ol>\n<p><p>\u5b8c\u6210\u67e5\u8be2\u540e\uff0c\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5173\u95ed\u8fde\u63a5<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528SQLAlchemy\u8fdb\u884c\u5339\u914d<\/h3>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Python SQL\u5de5\u5177\u5305\u548c\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff08ORM\uff09\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u5c42\u6b21\u7684API\u6765\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5SQLAlchemy<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5SQLAlchemy\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sqlalchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u521b\u5efa\u4e00\u4e2aSQLAlchemy\u5f15\u64ce\u5e76\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<h2><strong>\u521b\u5efaSQLAlchemy\u5f15\u64ce<\/strong><\/h2>\n<p>engine = create_engine(&#39;sqlite:\/\/\/example.db&#39;)<\/p>\n<h2><strong>\u521b\u5efaSession<\/strong><\/h2>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5b9a\u4e49\u6570\u636e\u5e93\u6a21\u578b<\/strong><\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u6570\u636e\u5e93\u8868\u7684\u6a21\u578b\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy import Column, Integer, String<\/p>\n<p>Base = declarative_base()<\/p>\n<p>class Table1(Base):<\/p>\n<p>    __tablename__ = &#39;table1&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True)<\/p>\n<p>    column_name = Column(String)<\/p>\n<p>class Table2(Base):<\/p>\n<p>    __tablename__ = &#39;table2&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True)<\/p>\n<p>    column_name = Column(String)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u67e5\u8be2\u5339\u914d\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528SQLAlchemy\u7684query\u51fd\u6570\u6765\u5339\u914d\u4e24\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy.orm import aliased<\/p>\n<h2><strong>\u521b\u5efa\u522b\u540d<\/strong><\/h2>\n<p>t1 = aliased(Table1)<\/p>\n<p>t2 = aliased(Table2)<\/p>\n<h2><strong>\u67e5\u8be2\u5339\u914d\u6570\u636e<\/strong><\/h2>\n<p>result = session.query(t1, t2).filter(t1.column_name == t2.column_name).all()<\/p>\n<h2><strong>\u6253\u5370\u5339\u914d\u7ed3\u679c<\/strong><\/h2>\n<p>for row in result:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u5173\u95edSession<\/strong><\/li>\n<\/ol>\n<p><p>\u5b8c\u6210\u67e5\u8be2\u540e\uff0c\u5173\u95edSession\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5173\u95edSession<\/p>\n<p>session.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5339\u914d\u4e24\u5217\u6570\u636e\u5e93\u3002<strong>\u4f7f\u7528pandas\u5e93\u6700\u4e3a\u7b80\u5355\u4e14\u76f4\u89c2\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff1b\u4f7f\u7528SQL\u67e5\u8be2\u66f4\u4e3a\u7075\u6d3b\u548c\u9ad8\u6548\uff0c\u9002\u5408\u590d\u6742\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff1b\u4f7f\u7528SQLAlchemy\u5219\u63d0\u4f9b\u4e86\u9ad8\u5c42\u6b21\u7684API\uff0c\u9002\u5408\u9700\u8981\u4f7f\u7528ORM\u7684\u9879\u76ee\u3002<\/strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8fdb\u884c\u6570\u636e\u5339\u914d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5\u548c\u5339\u914d\u4e24\u5217\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u8f7b\u677e\u8fde\u63a5\u548c\u5339\u914d\u4e24\u4e2a\u6570\u636e\u5e93\u8868\u4e2d\u7684\u6570\u636e\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5c06\u6570\u636e\u5e93\u4e2d\u7684\u8868\u52a0\u8f7d\u5230Pandas DataFrame\u4e2d\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u5229\u7528<code>merge<\/code>\u51fd\u6570\uff0c\u901a\u8fc7\u6307\u5b9a\u5217\u540d\u6765\u8fdb\u884c\u5339\u914d\u3002\u4f8b\u5982\uff0c<code>pd.merge(df1, df2, on=&#39;column_name&#39;)<\/code>\u5c06\u6839\u636e\u6307\u5b9a\u7684\u5217\u8fdb\u884c\u5339\u914d\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684DataFrame\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5e93\u5217\u5339\u914d\u65f6\uff0c\u662f\u5426\u9700\u8981\u5b89\u88c5\u7279\u5b9a\u7684\u5e93\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u8981\u5728Python\u4e2d\u8fdb\u884c\u6570\u636e\u5e93\u5217\u5339\u914d\uff0c\u60a8\u901a\u5e38\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u3002\u4f8b\u5982\uff0cPandas\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u80fd\u9ad8\u6548\u5730\u5904\u7406\u548c\u5339\u914d\u6570\u636e\u3002\u82e5\u6570\u636e\u5b58\u50a8\u5728SQL\u6570\u636e\u5e93\u4e2d\uff0c\u8fd8\u9700\u4f7f\u7528SQLAlchemy\u6216sqlite3\u7b49\u5e93\u6765\u8fde\u63a5\u6570\u636e\u5e93\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5339\u914d\u8fc7\u7a0b\u4e2d\u7684\u91cd\u590d\u6570\u636e\u6216\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728\u5339\u914d\u8fc7\u7a0b\u4e2d\uff0c\u5904\u7406\u91cd\u590d\u6570\u636e\u548c\u7f3a\u5931\u503c\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>drop_duplicates()<\/code>\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u884c\uff0c\u6216\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u586b\u8865\u7f3a\u5931\u503c\u3002\u6b64\u5916\uff0c<code>merge<\/code>\u51fd\u6570\u4e2d\u4e5f\u6709\u53c2\u6570\u53ef\u4ee5\u63a7\u5236\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\u548c\u91cd\u590d\u6570\u636e\uff0c\u4f8b\u5982\u4f7f\u7528<code>how=&#39;outer&#39;<\/code>\u3001<code>how=&#39;inner&#39;<\/code>\u7b49\u6765\u5b9a\u4e49\u8fde\u63a5\u7684\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5339\u914d\u4e24\u5217\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001SQL\u67e5\u8be2\u3001\u6216ORM\uff08\u5982SQLAlchemy\uff09\u3002\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1082198,"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\/1082190"}],"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=1082190"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082190\/revisions"}],"predecessor-version":[{"id":1082204,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082190\/revisions\/1082204"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1082198"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1082190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1082190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1082190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}