连接MySQL数据库,并读取数据:
1 import pymysql 2 import pandas as pd 3 4 #显示所有列 5 pd.set_option('display.max_columns', None) 6 #显示所有行 7 pd.set_option('display.max_rows', None) 8 #设置value的显示长度为100,默认为50 9 pd.set_option('max_colwidth', 100) 10 11 connection = pymysql.connect( 12 host='localhost', 13 user='root', 14 passwd='******', 15 db='hc_db', 16 charset='utf8' 17 ) 18 sql = 'select * from test' 19 df = pd.read_sql(sql, connection) 20 print(df)
连接MongoDB数据库,并读取数据:
1 from pymongo import MongoClient 2 import pandas as pd 3 4 #显示所有列 5 pd.set_option('display.max_columns', None) 6 #显示所有行 7 pd.set_option('display.max_rows', None) 8 #设置value的显示长度为100,默认为50 9 pd.set_option('max_colwidth', 100) 10 11 # 创建连接 12 client = MongoClient('localhost', 27017) 13 db = client.practice_db # 连接数据库 14 collection = db.gzDanKeZuFang # 连接数据集合 15 df = pd.DataFrame(list(collection.find())) 16 del df['_id'] 17 print(df)