- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我这样做
from sqlalchemy import create_engine
import pandas as pd
engine = create_engine('sqlite://')
conn = engine.connect()
conn.execute("create table test (a float)")
for _ in range(5):
conn.execute("insert into test values (NULL)")
df = pd.read_sql_query("select * from test", engine)
#df = pd.read_sql_table("test", engine)
df.a
结果是一列 None
值,而不是 float("nan")
。如果您逐 block 读取具有 NULL 值的浮点列,这会非常烦人。
read_sql_table
版本工作正常,因为我想它可以使用类型信息。
有没有一种简单的方法可以调整 read_sql_query
以将 NULL
值也解释为 float("nan")
?
最佳答案
好像an issue根据 wesm 在链接页面中的评论,提出了类似的东西 - coerce_float
参数 - 在版本 0.7.2 中被添加到 pandas:
hi arthur, I added an option coerce_float (in the above commit) that converts Decimal -> float and fills None with NaN. Converting Decimal to float is still really slow. Will be part of 0.7.2 to be released soon
虽然pandas.read_sql_query 0.18.1 docs中的描述似乎令人困惑:
coerce_float : boolean, default True
Attempt to convert values to non-string, non-numeric objects (like decimal.Decimal) to floating point, useful for SQL result sets
关于python - 调整pandas read_sql_query NULL值处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30652457/
刚刚开始学习 Pandas 并偶然发现了“read_sql_query”: 在我的代码中有多个创建语句,并使用“read_sql_query”执行它们会使作业变得多余,例如: import panda
是否可以使用炼金术 session (https://stackoverflow.com/a/33648514)调用 pandas.read_sql_query?示例: with db_session
我正在使用 pandas.read_sql_query 从 Sql 服务器读取一些数据。我读取的数据类型是Sql server中的int和real,即32位整数和32位浮点值。但在生成的数据框中,数据
当我这样做 from sqlalchemy import create_engine import pandas as pd engine = create_engine('sqlite://') c
我有一个封闭的连接: Config = dict(server = serverSample, port = 1433, username =
问题 我可以使用 read_sql() 将 SQL 事务提交到数据库吗? 用例和背景 我有一个用例,我希望允许用户执行一些预定义的 SQL 并返回一个 Pandas 数据帧。在某些情况下,此 SQL
我正在使用 Python(Jupyter Notebook)和 Postgres 数据库,并且正在努力填充 Pandas 数据框。 sql 代码使用 pgAdmin4 中的查询生成器运行良好 SELE
我正在尝试在 python pandas 中组合一个 SQL 查询。我尝试了不同的方法,但总是出现以下错误:提供的绑定(bind)数量不正确。当前语句使用 6 个,提供了 3 个。 我的代码如下。我做
我有两个表:trainSearcStream 和 SearchInfo。我的代码应该根据名为 SearchID 的公共(public)列合并这两个表。问题是代码运行了一段时间然后终止而没有在我的 ec
我正在使用 pandas read_sql_query 将数据从 MySQL 数据库表读取到 pandas 数据帧中。此表中的某些列全部为 NULL 值。对于这些列, Pandas 数据框的每一行都包
我有以下行 df = pandas.read_sql_query(sql = sql_script, con=conn, coerce_float = False) 使用 sql 脚本从 Postgr
我想使用类似于 pandas“read_sql_query”的东西,将数据库中的数据加载到 numpy 数组中,而不是 pandas 数据帧中。 在 pandas 中我使用类似的东西: df = pd
我有一个带有字符串类型列的 df,单元格可能是空字符串“”(中间没有空格)。我想将 df 写入 mysql,如下所示: df.to_sql(name, conn) 然后我想查询 df 并过滤掉带有空字
从 MySQL 数据库检索数据时,Pandas parse_date 应该如何工作? Pandas 0.23 的文档提供此信息: parse_dates : list or dict, default
我有一个包含以下列的表格: | Date | ProductId | SubProductId | Value | 我正在尝试从该表中检索数据并将其放入 pandas DataFrame 中。 我希望
在处理数据库 decimal 数据类型时,我遇到了 Pandas read_sql_query() 函数的问题。在使用 varchar 或 integer 类型时,我对下面相同的代码没有任何问题。 版
我试图在 sql 查询中传递三个变量。这些是地区、功能、新用户。我正在使用 SQL 驱动程序 SQL Server Native Client 11.0。 这是我的有效代码。 query = "SEL
我想执行一个操作,我想删除 postgres 中的所有行(但不删除表)并更新其中的新行。我想使用 pandas 的 pd.read_sql_query() 方法: qry = 'delete from
我一直在尝试测试使我的代码运行的各种方法。首先,我有这个列表: member_list = [111,222,333,444,555,...] 我试图将它传递到这个查询中: query = pd.re
我正在使用 pandas.read_sql_query 函数读取一些 sql 文件。一个查询在我挑出的某一特定位处抛出错误。 (python 位 - 没有什么奇怪的并且可以与其他查询一起使用) @co
我是一名优秀的程序员,十分优秀!