【发布时间】:2017-03-22 09:22:09
【问题描述】:
我想加入两个数据源,订单和客户:
orders 是一个 SQL Server 表:
orderid| customerid | orderdate | ordercost
------ | -----------| --------- | --------
12000 | 1500 |2008-08-09 | 38610
customers 是一个 csv 文件:
customerid,first_name,last_name,starting_date,ending_date,country
1500,Sian,Read,2008-01-07,2010-01-07,Greenland
我想在我的 Python 应用程序中加入这两个表,所以我编写了以下代码:
# Connect to SQL Sever with Pyodbc library
connection = pypyodbc.connect("connection string here")
cursor=connection.cursor();
cursor.execute("SELECT * from order)
result= cursor.fetchall()
# convert the result to pandas Dataframe
df1 = pd.DataFrame(result, columns= ['orderid','customerid','orderdate','ordercost'])
# Read CSV File
df2=pd.read_csv(customer_csv)
# Merge two dataframes
merged= pd.merge( df1, df2, on= 'customerid', how='inner')
print(merged[['first_name', 'country']])
我期待
first_name | country
-----------|--------
Sian | Greenland
但我得到空结果。
当我对都来自 CSV 文件的两个数据帧执行此代码时,它工作正常。有什么帮助吗?
谢谢。
【问题讨论】: