我有一个数据框架,我试图从一些元素的前面删除一个短语:
扇区
因此,当元素以“富时全股板块”开头时,我希望去掉这句话。
扇区
我试过了
df.Sector = df.Sector.map(lambda x: x.lstrip('FTSE All-Share Sector '))在某些情况下有效,但在另一些情况下则不起作用。
因此,我想它是在处理“富时全股”中的每一个角色,而不是用词来表达。
我也试过
df.Sector.replace (["FTSE All-Share Sector "],[""])它运行但没有明显的效果
和
if df.Sector.str.startswith('FTSE All-Share Sector '):
df.Sector = df.Sector[-24:]生成以下错误:
Traceback (most recent call last):
File "C:\Users\Alan\Downloads\eclipse-standard-kepler-SR1-win32-x86_64\eclipse\plugins\org.python.pydev_3.2.1.201401262345\pysrc\pydev_runfiles.py", line 466, in __get_module_from_str
mod = __import__(modname)
File "C:/Users/Alan/workspace/Data analysis/Tests\import.py", line 57, in <module>
if df.Sector.str.startswith('FTSE All-Share Sector '):
File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 665, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
ERROR: Module: import could not be imported (file: C:\Users\Alan\workspace\Data analysis\Tests\import.py).谢谢提前,希望这是一个简单的解决办法!
发布于 2014-02-07 18:01:27
您可以使用str.replace
df.Sector = df.Sector.str.replace ("FTSE All-Share Sector ", "")https://stackoverflow.com/questions/21634466
复制相似问题