python 操作数据库-pandas篇

python 操作数据库-pandas

# -*- coding: utf-8 -*-
# @Author: xiaodong
# @Date  : 2020/4/5

from collections import Counter
from functools import reduce

import pymysql
import pandas as pd
from sqlalchemy import create_engine


if __name__ == '__main__':
    config = {
        "host": "localhost",
        "port": 3306,
        "user": "root",
        "database": "movies",
        "charset": "utf8",
    }

    conn = pymysql.connect(**config)

    # read from database
    movies = pd.read_sql("select * from movies", conn)
    print(movies.head())

    # 统计genres
    print(Counter(reduce(lambda x, y: x + y,
                         map(lambda obj: obj.split("|"),
                             movies.genres.tolist()))))
    # >>> output-->
    # Counter({'Adventure': 15,
    #          'Animation': 3,
    #          'Children': 13,
    #          'Comedy': 37,
    #          'Fantasy': 5,
    #          'Romance': 24,
    #          'Drama': 53,
    #          'Action': 18,
    #          'Crime': 17,
    #          'Thriller': 22,
    #          'Horror': 5,
    #          'Mystery': 6,
    #          'Sci-Fi': 6,
    #          'War': 3,
    #          'Musical': 2,
    #          'Documentary': 3})

    # write to database
    engine = create_engine("mysql+pymysql://root:@localhost:3306/movies", echo=True)
    movies.to_sql("movies2", con=engine, if_exists="fail")

你可能感兴趣的:(pandas,数据库)