python-3.x - PandaSQL 很慢

标签 python-3.x pandas pandasql

我目前正在从 R 切换到 Python(anconda/Spyder Python 3)以进行数据分析。在 R 中,我曾经使用过很多 R sqldf。由于我擅长sql查询,我不想重新学习data.table语法。使用 R sqldf,我从来没有遇到过性能问题。

现在,在 Python 中,我尝试使用 pandasql 一个简单的 df = "SELECT * From table LIMIT 1"将永远持续 193k 行,19 列。

我试过 pysqldf 但我收到一个错误,说该表不存在,但它确实存在。

# -*- coding: utf-8 -*-

import pandas as pd
import pandasql 
import pysqldf

#Data loading    
orders = pd.read_csv('data/orders.csv',sep = ';')

###### PANDASQL ######
test = pandasql.sqldf("SELECT  orders_id from orders LIMIT 1;",globals())
# Will last several minutes and use a lot of RAM

test = pandasql.sqldf("SELECT  orders_id from orders LIMIT 1;",locals())
# Will last several minutes and use a lot of RAM


###### PYSQLDF ######
sqldf = pysqldf.SQLDF(globals())
test = sqldf.execute("SELECT  * from orders LIMIT 1;")
#error
#Error for pysqldf

Traceback (most recent call last):

  File "<ipython-input-12-30b645117dc4>", line 1, in <module>
    test = sqldf.execute("SELECT  * from orders LIMIT 1;")

  File "C:\Users\p.stepniewski\AppData\Local\Continuum\anaconda3\lib\site-packages\pysqldf\sqldf.py", line 76, in execute
    self._del_table(tables)

  File "C:\Users\p.stepniewski\AppData\Local\Continuum\anaconda3\lib\site-packages\pysqldf\sqldf.py", line 117, in _del_table
    self.conn.execute("drop table " + tablename)

OperationalError: no such table: orders

我错过了什么吗?在“学习 Pandas 查询语法”之前更喜欢pandasql/pysqldf答案。

R 中的 Sqldf 在 i7/12G ram 笔记本电脑上处理多达 1000 万行的表的复杂查询。

谢谢 !

最佳答案

好的,刚刚找到了解决方案。

  • 完全放弃了 Anaconda 安装。
  • 清理相关文件夹。
  • 使用 PIP 从头开始​​安装 Python 3.6。
  • 然后pip安装pandas,pandasql。
  • 启动了我的脚本。脚本在不到一秒内执行 (pandasql)
  • 关于python-3.x - PandaSQL 很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51590671/

    相关文章:

    python - 如何在python 3中正确使用@property装饰器?

    python - 如何使用pymssql将数据帧写入mssql?

    python - 使用 pandas 跟踪行中元素的位置

    python - 如何将子流程输出保存为字符串列表以供进一步分析?

    python-3.x - 如何从spacy vocab中获取所有单词?

    python - 查询 panda df 以过滤列不是 Nan 的行

    mysql - 如何使用并行插入语句在 MySQL 表中插入巨大的 Pandas Dataframe?

    python - 查找以 1 分钟为间隔采样的 pandas 时间序列数据帧中的空白,并用新行填充空白

    python - 如何在 pandas 中进行条件转置(类似于使用 group by 时的 SQL 情况)