gpt4 book ai didi

python - 使用 pandas 和 sqldf 没有这样的表错误

转载 作者:太空狗 更新时间:2023-10-30 00:19:06 24 4
gpt4 key购买 nike

我收到 sqlite3 错误。

OperationalError: no such table: 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)

感谢您花时间阅读本文。

最佳答案

您是否尝试将 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)

关于python - 使用 pandas 和 sqldf 没有这样的表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39780443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com