【发布时间】:2020-01-08 00:51:28
【问题描述】:
我在 python 中有一个字符串。从这个字符串,我想写一个函数,它返回整个字符串,直到(没有)第三个逗号。
import pandas as pd
import numpy as np
mystr = pd.Series(['culture clash, future, space war, space colony, society',
'ocean, drug abuse, exotic island, east india, love, traitor])
def transform(s):
index = 0
count = 0
while count < 3:
index = s.str.find(',', index)
count = count+1
index += 1
return s.str[0:index-1]
out = transform(mystr)
out
这将返回 NaN。我想要:
- '文化冲突、未来、太空战争'
- '海洋,吸毒,异国岛屿'
谁能帮我解决这个问题?
【问题讨论】:
标签: python string pandas indexing