跳转到主要内容

pandas && 强大的数据操作处理程序

安装


pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple

# excel支持
pip install openpyxl -i https://pypi.tuna.tsinghua.edu.cn/simple

连接到数据

支持读取多张数据:CSV,JSON,Excel,mysql等。

具体可以参考:https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html

api设计的非常简洁:

  • 读取:read_csv , read_json , read_excel,read_sql
  • 写入:to_csv , to_json,to_excel , to_sql
连接到mysql

pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install sqlalchemy -i https://pypi.tuna.tsinghua.edu.cn/simple 



# 增加pandas库的导入
import pandas as pd

# 添加SQLAlchemy的导入
from sqlalchemy import create_engine

# 示例用法
if True:
    engine = create_engine(
        "mysql+pymysql://root:password@192.168.0.10:33318/renren_cloud_basic"
    )
    query = "SELECT * FROM park_user"
    # 使用chunksize参数来实现流式处理
    df = pd.read_sql(query, engine)
    # 显示数据的结构和数据类型
    print(df.info())
    # 显示列名
    print(df.columns)



连接到mysql的额外操作
  • read_sql_table(table_name,connection)
  • read_sql_query(sql,connection)
  • read_sql(sql,connection)

操作数据

|||| ||||