读取csv中的数据,写入sqlite数据库的wine表中
from sqlalchemy import *import pandas as pddef csv_to_df(file):wine_df = pd.read_csv(file)# print(wine_df.head(10))return wine_dfdef df_to_sql(wine_df):"""接收dataframe为参数,写入数据库"""# 定义引擎,test.db为数据库名,./表示当前路径engine = create_engine('sqlite:///./test.db', echo=True)wine_df.to_sql('wine', engine) # 从score表读数据dataframeif __name__ == '__main__':filename = '../data/winemag-data-130k-v2.csv'winedata_df = csv_to_df(filename)df_to_sql(winedata_df)
从数据库中读取数据
from sqlalchemy import *import pandas as pddef df_to_sql():"""接收dataframe为参数,写入数据库"""# 定义引擎,test.db为数据库名,./表示当前路径engine = create_engine('sqlite:///./test.db', echo=True)print(pd.read_sql('wine',engine))if __name__ == '__main__':filename = '../data/winemag-data-130k-v2.csv'df_to_sql()
FROM wine2020-11-15 23:42:55,539 INFO sqlalchemy.engine.base.OptionEngine ()index ... winery0 0 ... Nicosia1 1 ... Quinta dos Avidagos2 2 ... Rainstorm3 3 ... St. Julian4 4 ... Sweet Cheeks... ... ... ...129966 129966 ... Dr. H. Thanisch (Erben Müller-Burggraef)129967 129967 ... Citation129968 129968 ... Domaine Gresser129969 129969 ... Domaine Marcel Deiss129970 129970 ... Domaine Schoffit[129971 rows x 15 columns]
