用递归函数的方式处理dataframe形式的数据,但是不明白为什么已经从rest_list中删除的数据仍然在被循环,导致了出现“没有办法将这行数据从dataframe中删去的报错”
def ExpandHeat(rest_list,row1):
global k,heat
for index,row in rest_list.iterrows():
row1 = row1.copy() #动态记录所在簇的情况
#当出现重叠部分
if ((row['start']>=row1['start'] and row['start']'end']) or (row['end']<=row1['end'] and row['end']>row1['start'])):
#以扩张的方式更新高热部分的始末位置
row1['start'] = min(row1['start'], row['start'])
row1['end'] = max(row1['end'], row['end'])
#若标记用户重复,合并评论内容
if row['username'] in row1['username']:
row1['content'] += (';' + row['content'])
print(row['index'],k)
#print(row1)
else:
row1['username'] += row['username']
heat[k] = row['index']
k += 1
print(row['index'],k)
print(heat)
#print(row1)
print('*****标记+1*****')
rest_list.drop([row['index']], inplace=True) #将处理过的这行从列表中删去
print('剩余列表长度:',len(rest_list['index']))
ExpandHeat(rest_list,row1) #递归执行新形成的高热簇
return k
s = 0 #高热内容的个数
hot = np.zeros(20, dtype=np.int) #存放所有高热内容
rest_list = group_data
while len(rest_list) >= 3:
#逐行循环没有处理过的
row1 = rest_list.iloc[0]
index = row1['index']
rest_list = group_data.drop([index])
k = 0 #某块内容的高热指数
heat = np.zeros(10, dtype=np.int) #存放某块高热内容
k = ExpandHeat(rest_list,row1)
#若高热指数满足条件,这一高热簇的索引列表加入总的高热内容列表中
if k>=3:
print('==========OK=========')
hot[s] = heat
s += 1
else:
print('!!!!!!!!!QUIT!!!!!!!!')
print(hot)
3 0
剩余列表长度: 70
25 1
[25 0 0 0 0 0 0 0 0 0]
标记+1
剩余列表长度: 69
31 1
剩余列表长度: 68
31 1
KeyError: '[31] not found in axis'