![](/img/trans.png)
[英]Python 3.6: Find first occurance string(entire column value) from dataframe which starts with '$'
[英]Python Dataframe find the index of a second occurance of a string
我看到有人提出了类似的问题,但那里的解决方案对我不起作用,因为我相信,我正在使用 dataframe 中的一个专栏。
我有一列,里面有一个字符串。 我找到了一个术语的第一次出现。 这行得通。 然后我想找到该术语的第二次出现。 这是行不通的。
我的代码
import pandas as pd
data = {"Text" : ["['one', 'one two']","['two one', 'three']"]}
df = pd.DataFrame(data)
#yes the data is in a list in a column but I treat it as a string
#finding if "one" is in the string - works
df["Ones"] = df.Text.str.find("one")
#finding if "one" is in the string another time as in the first row
df["NextOnes"] = df.Text.str.find("one",df.Ones +1)
“NextOnes”行返回 NA。 在我的真实代码中,它返回空白。 如果我用一个数字(例如 2)替换对该列的引用,那么这将返回正确的值。 但是,此值需要是动态的。
我刚刚得到了这种大海捞针的工作方法,但在这里构建 for 循环似乎效率低下,
for i in range(len(df)):
... df["Next"][i] = find_nth(str(df.Text[i]),"one",2)
您可以尝试使用 str 中的 find 方法,但传入前一个项目索引作为起点(作为要查找的参数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.