我有一个名为“Stories”的 DF,如下所示:
Story
The Man
The Man Child
The Boy of Egypt
The Legend of Zelda
有没有办法提取每个字符串中的最后一个单词?
就像是:
Stories['Prefix'] = final['Story'].str.extract(r'([^ ]*)')
找到前缀,但我不知道如何相应地调整它
我希望最终能得到类似的结果
Story Suffix
The Word Of Man Man
The Man of Legend Legend
The Boy of Egypt Egypt
The Legend of Zelda Zelda
任何帮助将非常感激!
您可以使用.str
两次,如.str[-1]
将拾取最后一个元素:
>>> df["Suffix"] = df["Story"].str.split().str[-1]
>>> df
Story Suffix
0 The Man Man
1 The Man Child Child
2 The Boy of Egypt Egypt
3 The Legend of Zelda Zelda
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)