Python 是否替换dataframe中的值-已更改的功能?
我正在研究如何替换数据帧中的值。我直接从文档中复制和粘贴代码来实现这一点,但它不起作用Python 是否替换dataframe中的值-已更改的功能?,python,pandas,Python,Pandas,我正在研究如何替换数据帧中的值。我直接从文档中复制和粘贴代码来实现这一点,但它不起作用 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
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
某些功能是否已更改?此功能适用于熊猫版
0.23.1
。如果要安装此版本,请使用以下命令
首先卸载熊猫
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'
与我们分享您的熊猫版本号。
pd.\u\u版本\u
或pip show pandas
.pd.0.25.0work@WeNYoBen这适用于pandas版本0.25.0
。请参阅我使用的0.23.4版的实时代码。安装0.23.1后,它就可以工作了。谢谢