gpt4 book ai didi

python - Pandas itertuple 返回不一致的类型,Pandas 或 tuple

转载 作者:行者123 更新时间:2023-12-01 01:55:50 25 4
gpt4 key购买 nike

我从我之前在较小的数据集上使用过多次的代码中得到了一种以前从未见过的奇怪行为。我正在使用 Pandas 数据帧 read_table 解析 VCF 文件。 VCF 文件有一个标题,然后是 9 列以及任意多列的个人。之前,当我使用 for row in Genomes_df.itertuples(): 来迭代数据帧的每一行时,我可以使用 row.SVLEN 来调用列“SVLEN”。当我检查 type(row) 时,它是一个 Pandas 对象。今天,我在相同 VCF 格式的较大文件(350 列与之前的 10 列)上运行脚本,它给了我 AttributeError: 'tuple' object has no attribute 'SVLEN' 因为现在 type(row) 是一个元组!

这是怎么回事?列名称不同(NWD107911.mark_dupesNWD107911),但我检查名称中没有空格(在另一篇文章中读到它可能会导致不同的行为)。

最佳答案

iterttuples documentation 中提到了这一点:

With a large number of columns (>255), regular tuples are returned.

在源码中可以看到here :

        # Python 3 supports at most 255 arguments to constructor, and
# things get slow with this many fields in Python 2
if name is not None and len(self.columns) + index < 256:
# `rename` is unsupported in Python 2.6
try:
itertuple = collections.namedtuple(name,
fields + list(self.columns),
rename=True)
return map(itertuple._make, zip(*arrays))
except Exception:
pass

注意:cpython 调用/命名元组的 255 个参数的限制已是 fixed in python 3.7 ,因此这可能会在 pandas 的 future 版本中发生变化(在 python 3.7+ 上运行)。

关于python - Pandas itertuple 返回不一致的类型,Pandas 或 tuple,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50205134/

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