gpt4 book ai didi

python - 替换 Pandas 数据框中的值 - 更改功能?

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:15 25 4
gpt4 key购买 nike

我正在查看 pandas documentation关于如何替换数据框中的值。我直接从文档中复制并粘贴代码来执行此操作,但它不起作用。

df = pd.DataFrame({'A': ['bat', 'foo', 'bait'],
'B': ['abc', 'bar', 'xyz']})

print(df)

Result:
A B
0 bat abc
1 foo bar
2 bait xyz

df.replace(to_replace=r'^ba.$', value='new', regex=True)

Result:
A B
0 bat abc
1 foo bar
2 bait xyz

某些功能是否发生了变化?

最佳答案

这适用于 pandas 版本 0.23.1。如果要安装此版本,请使用以下命令。

先卸载pandas

pip uninstall pandas

然后安装工作版本。

pip install pandas=0.23.1

我已经测试了代码,这是我得到的输出。

df = pd.DataFrame({'A': ['bat', 'foo', 'bait'],
'B': ['abc', 'bar', 'xyz']})

>>> df.replace(to_replace=r'^ba.$', value='new', regex=True)
A B
0 new abc
1 foo new
2 bait xyz

>>> pd.__version__
'0.23.1'

关于python - 替换 Pandas 数据框中的值 - 更改功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57166254/

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