【问题标题】:pandas mysql How to update some columns of rows using a Dataframepandas mysql如何使用Dataframe更新一些行列
【发布时间】:2022-01-22 21:29:07
【问题描述】:

我的数据格式为:

list_data = [{'id': '1', 'city': 'Tokyo', 'country': 'Japan'},
             {'id': '2', 'city': 'Noida', 'country': 'India'},
             {'id': '3', 'city': 'Seoul', 'country': 'South korea'}]

df_data = pd.Dataframe(list_data)

数据库表:

id colour city code country
1 white 125
2 red 48
3 pink 56
4 yellow 456
5 white 213

这是一个例子。通常会有更多行需要更新。
我想更新 id 为 1、2、3 的行的“城市”和“国家”列。

一次更新数据库表的代码是什么?

【问题讨论】:

  • 您可以使用事务。
  • @Zaki 你能详细说明一下吗?我是新手,所以对某些术语不太熟悉。

标签: python mysql pandas sqlalchemy


【解决方案1】:

如果您将使用 pandas 来写入数据库,那么您会发现使用 SQLAlchemy 而不是原始 DBAPI 连接更有优势。在这种情况下:

from pprint import pprint

import pandas as pd
import sqlalchemy as sa

engine = sa.create_engine("mysql+mysqldb://scott:tiger@localhost:3307/mydb")

# create the test environment
#
with engine.begin() as conn:
    conn.exec_driver_sql("DROP TABLE IF EXISTS table1")
    conn.exec_driver_sql(
        """
        CREATE TABLE table1 (
        id int primary key,
        colour varchar(50),
        city varchar(50),
        code varchar(50),
        country varchar(50)
        )
        """
    )
    conn.exec_driver_sql(
        """
        INSERT INTO table1 (id, colour, code) VALUES 
        (1, 'white', '125'),
        (2, 'red', '48'),
        (3, 'pink', '56'),
        (4, 'yellow', '456'),
        (5, 'white', '213')
        """
    )

list_data = [
    {"id": "1", "city": "Tokyo", "country": "Japan"},
    {"id": "2", "city": "Noida", "country": "India"},
    {"id": "3", "city": "Seoul", "country": "South korea"},
]
df_data = pd.DataFrame(list_data)

# run the test
#
with engine.begin() as conn:
    sql = """
    UPDATE table1 SET city = :city, country = :country
    WHERE id = :id
    """
    params = df_data.to_dict("records")
    conn.execute(sa.text(sql), params)

    pprint(conn.exec_driver_sql("SELECT * FROM table1").fetchall())
    """
    [(1, 'white', 'Tokyo', '125', 'Japan'),
     (2, 'red', 'Noida', '48', 'India'),
     (3, 'pink', 'Seoul', '56', 'South korea'),
     (4, 'yellow', None, '456', None),
     (5, 'white', None, '213', None)]
    """

【讨论】:

    【解决方案2】:

    您可以使用 prepared statementexecutemany 方法,例如

    import pandas as pd
    import mysql.connector
    from mysql.connector import Error
    
    con = mysql.connector.connect(host='localhost',
                                  database='mydbname',
                                  user='myschema',
                                  password='mypwd')
    
    cur = con.cursor()
    
    list_data = [{'id': '1', 'city': 'Tokyo', 'country': 'Japan'},
    {'id': '2', 'city': 'Noida', 'country': 'India'},
    {'id': '3', 'city': 'Seoul', 'country': 'South korea'}]
    
    df_data = pd.DataFrame(list_data)
    val = df_data[["city","country","id"]].values.tolist()
    
    qry = "UPDATE tab SET city = %s, country = %s WHERE id = %s"
    cur.executemany(qry, val)
    con.commit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      相关资源
      最近更新 更多