【发布时间】:2021-09-22 05:15:37
【问题描述】:
我有一个我在 python 中读取的数据。在“标题”列中,很少有行具有我想要删除的“新”等额外字符。我试图找到正确的代码,但我找不到任何代码,当我尝试自己的代码时,我得到了错误。任何人都可以帮忙!!!提前致谢。 title data
if indeed['title'] == indeed.loc[indeed['title'].str.startswith('new')].copy():
indeed['title'].str[3:]
错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-34-486b16b22bea> in <module>
----> 1 if indeed['title'] == indeed.loc[indeed['title'].str.startswith('new')].copy():
2 indeed['title'].str[3:]
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __nonzero__(self)
1325 def __nonzero__(self):
1326 raise ValueError(
-> 1327 f"The truth value of a {type(self).__name__} is ambiguous. "
1328 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1329 )
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
【问题讨论】:
-
嗨@HenryEcker 非常感谢您的帮助。
-
链接的副本有更好的选择
indeed['title'] = indeed['title'].str.extract('(?:new)?(.*)', expand=False)或indeed['title'] = indeed['title'].str.replace('^new', '', regex=True)同样,您接受的答案也有效。