pandas操作excel操作-20-操作数据库-复杂函数的使用

核能气质少年 提交于 2020-03-01 23:40:17

读取数据库

import pandas as pd
import pyodbc
import sqlalchemy

connection = pyodbc.connect('{driver_url}')
engine = sqlalchemy.create_engine('{driver_url}')

query_sql = 'select * from user'

df1 = pd.read_sql_query(query_sql, connection)
df2 = pd.read_sql_query(query_sql, engine)

复杂函数的使用,结构化编写复杂函数

import pandas as pd
import numpy as np

def get_circumcircle_area(l, h):
	r = np.sqrt(l ** 2 + h ** 2)/2
	return r ** 2 * np.pi

def wrapper(row):
	return get_circumcircle_area(row['Length'], row['Height'])

rects = pd.read_excel('C:/rects.xlsx', index_col='ID')
rects['CA'] = rects.apply(wrapper, axis=1)
print(rects)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!