我正在从文本文件阅读值,并尝试查找子字符串的索引,如下所示
df=pd.read_csv('break_sent.txt', index_col=False,encoding='utf-8',delimiter="\n",names=['sent'])
#print(df[:50])
#df.index = list(df.index)
df1= df[40:50]
print(len(df))
print(df1.index)
print("-------------------------------------------")
for i,row in df1.iterrows():
string = row['sent']
#print("string",string)
d = df1[df1.sent.str.match(string)] # if the result includes more than 1 value then we know that substring and its matching parent string are present, then I will eliminate the substring from the dataframe
if len(d.index > 2):
index_val = df.index(string)
df.drop(df.index(string),inpace=True)
df.reset_index(level=None, drop=True, inplace=True)
当我运行这段代码时,我收到以下错误
Traceback (most recent call last):
File "process.py", line 15, in <module>
index_val = df.index(string)
TypeError: 'RangeIndex' object is not callable
我尝试将范围索引转换为List
df.index = list(df.index)
但是Int64Index是不可调用的,怎么才能得到字符串的索引呢?
2条答案
按热度按时间1rhkuytd1#
你换吧
到
oxcyiej72#
你需要在 Dataframe 上运行
df.index
,而不是在你的搜索字符串上。将为您提供与字符串匹配的行,然后您应该能够将输出传递给
df.drop
:我可能还没有掌握你要做的事情的确切细节,但希望这能给你指明正确的方向。