pandas——改写pandas源文件以实现:使用pd.DataFrame.itertuples但不自动修正列名

使用pd.DataFrame.itertuples不自动修正列名

何为pandas.DataFrame.itertuples?

相较于 pandas.DataFrame.iterrows 而言,pandas.DataFrame.itertuples更好地提供了按行遍历DataFrame 的功能,详见pandas------按行遍历dataframe的优选方法(itertuples,iterrows)

这这里,我们需要了解的是,itertuples返回的是一个namedtuple迭代器。同时可以传递两个参数:name和index。其中,index决定了是否包含索引,而name决定了namedtuple的名称。

何为namedtuple?

那么到这里,就需要提到关于namedtuple的基本信息。

可以查看python------什么是namedtuple?了解它,理解它,掌握它

一旦了解过namedtuple后,我们就可以知道namedtuple有一个参数:rename。这个参数决定了是否将无效的字段名自动替换为位置名称。

问题所在

先看一下pandas.DataFrame.itertuples的源代码,方便起见我只摘取需要关注的一部分:

python 复制代码
def itertuples(
        self, index: bool = True, name: str | None = "Pandas", rename: bool = True
    ) -> Iterable[tuple[Any, ...]]:
    arrays = []
        fields = list(self.columns)
        if index:
            arrays.append(self.index)
            fields.insert(0, "Index")
            arrays.extend(self.iloc[:, k] for k in range(len(self.columns)))

        if name is not None:
            # https://github.com/python/mypy/issues/9046
            # error: namedtuple() expects a string literal as the first argument
            itertuple = collections.namedtuple(  # type: ignore[misc]
                name, fields, rename=True
            )
            return map(itertuple._make, zip(*arrays))

        # fallback to regular tuples
        return zip(*arrays)

如上所示,它直接定义了rename=True,也就默认让itertuples自动修正无效的字段名。

那么我们就需要将rename参数重新在itertuples中恢复,同时也不能影响pandas的正常使用

解决办法

直接将 rename=True改为rename=False即可。

友情提示

rename这个参数为True时,可以自动修正。但为False时,如果列名不符合python的命名规则,直接报错,并不是按照原名称进行输出。所以在使用时谨慎使用。

相关推荐
2301_809204701 天前
bootstrap怎么实现鼠标悬停切换图片预览功能
jvm·数据库·python
小徐学编程-zZ1 天前
量产测试数据
python·压力测试·数据库架构
QQ8057806511 天前
django基于机器学习的电商评论情感分析系统设计实现
python·机器学习·django
wx09091 天前
stata实现机器学习的环境配置
python·机器学习·stata
nuowenyadelunwen1 天前
CS 61A Lab 2 笔记:短路求值、高阶函数与 Lambda 表达式
python·函数式编程·cs61a·berkeley
qq_422828621 天前
android图形学之SurfaceControl和Surface的关系 五
android·开发语言·python
weixin_444012931 天前
c++如何将std--vector直接DUMP到二进制文件_指针地址直写【附代码】
jvm·数据库·python
woxihuan1234561 天前
Go语言中--=运算符详解:位右移赋值操作的原理与应用
jvm·数据库·python
石山代码1 天前
Python 数据分析三大库:NumPy + Pandas + Matplotlib
python·数据分析·numpy
如竟没有火炬1 天前
用队列实现栈
开发语言·数据结构·python·算法·leetcode·深度优先