首页
学习
活动
专区
工具
TVP
发布

pandas 统计分析基础

读/写不同数据源的数据

读取订单数据库数据

读取订单信息表CSV数据

读取客户信息Excel数据

数据库读取有3个函数,分别是pandas.read_sql_table、pandas.read_sql_query和pandas.read_sql。

read_sql_table只能读取数据库的某一个表格,不能实现查询操作。read_sql_query则只能实现查询操作,不能读取数据库中的某个表。read_sql是两者的综合,能读取数据库的某一个表,也能实现查询操作。

pandas.read_sql_table(table_name,con,schema=None,index_col=None,corece_float=True,column=None)

pandas.read_sql_query(sql,con,index_col=None,coerce_float=True)

pandas.read_sql(sql,con,index_col=None,coece_fkiat=True,columns=None)

数据库连接字符串的格式如下:

数据库产品名+连接工具名://用户名:密码@数据库IP地址:数据库端口号/数据库名称?charset=数据库编码

SQLAlchemy连接MySQL数据库的代码

fromsqlalchemyimportcreate_engine

engine=create_engine('mysql+pymysql://root:linzhiyang@127.0.0.1:3306/data/?charset=utf8')

print(engine)

在creat_engine中输入的是一个连接字符串

read_sql_table、read_sql_query、read_sql 函数参数及其重要参数说明

sql or table_name:接收string。表示读取的数据的表面或SQL语句。无默认

con:接收数据库连接。表示数据库连接信息。无默认

使用read_sql_table、read_sql_query、read_sql函数读取数据库数据

importpandasaspd

formlist=pd.read_sql_query('show tables',con=engine)#使用read_sql_query查看data中的数据表数目

print(formlist)

输出结果:

Tables_in_data

meal_order_detail1

1meal_order_detail2

2meal_order_detail3

detail1=pd.read_sql_table('meal_order_detail1',con=engine)

detail2=pd.read_sql('select * from meal_order_detail2',con=engine)

detail3=pd.read_sql('select * from meal_order_detail3',con=engine)

print("使用read_sql_table函数 读取长度为:",len(detail1))

print("使用read_sql函数 读取长度为:",len(detail2))

print("使用read_sql函数 读取长度为:",len(detail3))

输出结果:

使用read_sql_table函数读取长度为:2779

使用read_sql函数读取长度为:3647

使用read_sql函数读取长度为:3611

数据库数据存储

to_sql使用语法如下

DataFrame.to_sql(name, con, schema=None ,if_exists=‘fail’, index=True, index_label=None, dtype=None)

detail.to_sql('test1',con=engine,index=False,if_exists='replace')

formlist1=pd.read_sql_query('show tables',con=engine)

print(formlist1)

输出结果:

Tables_in_data

0 meal_order_detail1

1 meal_order_detail2

2 meal_order_detail3

3 test1 #

CSV文件 读/写(read_csv&to_csv)

pandas.read_csv(filepath,sep=‘,’,header=‘infer’,names=None,index_col=None,dtype=None,encoding=utf-8,engine=None,nrows=None)

pandas.to_csv(path_or_buf=None,sep=‘,’,na_rep=‘’,columns=None,header=True,index=True,index_label=None,mode=‘w’,encoding=None)

order=pd.read_csv('../data/data/chapter4/meal_order_info.csv',encoding='gbk')

print("读取长度为:",len(order))

读取长度为:945

order.to_csv('./orderInfo.csv',encoding='gbk')

order2=pd.read_csv('./orderInfo.csv',encoding='gbk')

print("读取长度为:",len(order2))

读取长度为:945

Excel文件 读/写(read_excel&to_excel)

pandas.read_excel(io,sheetname=0,header=0,index_col=None,names=None,dtype=None)

DataFrame.to_excel(excel_writer=None,sheetname=‘None’,na_rep=‘’,header=True,index=True,index_label=None,mode=‘w’,encoding=None)

user=pd.read_excel('../data/data/chapter4/users.xlsx')

print("读取长度为:",len(user))

user.to_excel('./userInfo.xlsx')

print("读取长度为:",len(pd.read_excel('./userInfo.xlsx')))

读取长度为:734

读取长度为:734

中秋节快乐哈!

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180924G0Y7NS00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券