使用pandas和sqldf获取此类表错误

时间:2016-09-29 21:38:38

标签: python sql pandas dataframe sqldf

我收到一个sqlite3.OperationalError:没有这样的表:Bills

我首先使用pandas调用我的数据帧,然后在我的查询中调用那些工作正常的数据帧

import pandas as pd
from pandasql import sqldf

Bills = pd.read_csv("Bills.csv")
Accessorials = pd.read_csv("Accessorials.csv")

q = """
Select          
            CityStateLane, 
            Count(BillID) as BillsCount, 
            Sum(BilledAmount) as BillsSum, 
            Count(Distinct CarrierName) as NumberOfCarriers, 
            Avg(BilledAmount) as BillsAverage, 
            Avg(BilledWeight) as WeightAverage
From 
            Bills
Where 
            Direction = 'THIRD PARTY' 
Group by 
            CityStateLane
Order by 
            BillsCount DESC
"""

topCityStateLane = sqldf(q)

然后我使用另一个查询创建另一个数据框但是这会调用错误,说Bills不存在,即使我在上一个查询中成功使用它。

q = """
SELECT
         Bills.BillID as BillID,
         A2.TotalAcc as TotalAcc
FROM
            (SELECT
                    BillID_Value,
                    SUM(PaidAmount_Value) as "TotalAcc"
            FROM  
                    Accessorials 
            GROUP BY
                    BillID_Value 
            ) AS  A2,
            Bills 
WHERE    
            A2.BillID_Value  = Bills.BillID
 """
temp = sqldf(q)

感谢您抽出宝贵时间阅读本文。

2 个答案:

答案 0 :(得分:0)

您是否要将Bills与A2表一起使用?您不能在一个select from语句中从两个表中选择列。

q = """
SELECT
         Bills.BillID as BillID,
         A2.TotalAcc as TotalAcc
FROM
            (SELECT
                    BillID_Value,
                    SUM(PaidAmount_Value) as "TotalAcc"
            FROM  
                    Accessorials 
            GROUP BY
                    BillID_Value 
            ) AS  A2 
            join Bills 
            on A2.BillID_Value  = Bills.BillID
 """
temp = sqldf(q)

答案 1 :(得分:0)

            ) AS  A2,
        Bills 

我认为这是您的问题所在。您不是在FROM子句中调用Bills表,而是在通过Alas A2编写的子查询中调用返回表。换句话说,您的From子句指向A2“表”而不是Bills。正如王千波提到的,如果要从这两个单独的表中返回输出,则必须将它们连接在一起。