python pandas,替换函数一次应用于多个数据帧

标签 python pandas

我有一个字典,例如:

elements_to_replace= {r'.\(MoM\)': ' MoM', 
                      r'.% m/m': ' MoM', 
                      r'.\(QoQ\)': ' QoQ', 
                      r'.\(WoW\)': ' WoW'}

和 3 个数据帧 df1df2df3。目前我在所有 3 个数据帧上使用替换函数并且它有效。

df1.replace(elements_to_replace, regex=True)
df2.replace(elements_to_replace, regex=True)
df3.replace(elements_to_replace, regex=True)

有没有办法同时对这 3 个数据帧应用替换功能?

最佳答案

请注意pd.DataFrame.replace默认情况下,不会修改 DataFrame,它只是返回修改后的副本

不过,使用 inplace 参数,您可以执行以下操作:

for df in [df1, df2, df3]:
    df.replace(elements_to_replace, regex=True, inplace=True)

接下来,df1df2df3 应该进行修改。

关于python pandas,替换函数一次应用于多个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41340832/

相关文章:

python - 如何使用 python pandas 替换列中的特定数字。

python - 尝试根据列是否包含特定字符串对其进行重新编码

python - Pandas 将用户代理列解析为多列

python - 为什么默认情况下 pip 包没有安装到 $PATH?

python - Golang 与 Python - 十六进制字符串到 Int

python - PyObjC 在 python 中调用带有参数(按值传递)的 Objective-C 函数

python - 使用破折号下拉列表过滤 pd.DataFrame

python - GAE : How to get the blob-image height

python - 这个命令到底是做什么的?

python - 计算两个字符串之间的唯一 ID 重叠