【发布时间】:2021-10-26 05:34:21
【问题描述】:
df = pd.DataFrame({'a': ['asdf']}, dtype="string")
df["a"].replace({"a":"b"}, regex=True)
not chagend
df = pd.DataFrame({'a': ['asdf']}, dtype="object")
df["a"].replace({"a":"b"}, regex=True)
changed
我想将字符串值转换为其他值。 但是,如果我使用类型字符串,我不能使用替换方法。 如何更改字符串类型数据? 我应该使用对象类型吗?
【问题讨论】:
-
我尝试了第一个代码,它成功了;将 a 更改为 b。熊猫 1.3 版
-
afternoon_drinker,你需要在字符串
dtype上使用.str方法,详情请看答案。 -
@sammywemmy,谢谢。我用的是 1.2.4
标签: python pandas string dataframe