gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:26:45 26 4
gpt4 key购买 nike

我有一个字典,例如:

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/

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