从循环打印语句隐藏到熊猫数据框

时间:2020-07-06 17:14:37

标签: python pandas

我有一个以下循环,我想将其转换为数据帧。我看到了old related-question。但这不能解决我的问题

closest_n = 10
for query, query_embedding in zip(queries, query_embeddings):
    distances = scipy.spatial.distance.cdist([query_embedding], corpus_embeddings, "cosine")[0]
    results = zip(range(len(distances)), distances)
    results = sorted(results, key=lambda x: x[1])

for idx, distance in results[0:closest_n]:
    print("Score:   ", "(Score: %.4f)" % (1-distance) , "\n" )
    print("content:   ", corpus[idx].strip(), "\n" )
    row_di = dfc2.loc[dfc2.index== corpus[idx]].to_dict()
    print("date:  " , row_di["date"][corpus[idx]] , "\n")
    print("Title:  " , row_di["title"][corpus[idx]] , "\n")
    print("-------------------------------------------")

我的尝试

d = []  
for idx, distance in results[0:closest_n]:
  d.append(
      {'score':(1-distance),
       'paragraph':corpus[idx].strip(),
       #'row_dic' : dfc2.loc[dfc2.index== corpus[idx]].to_dict()
          
      }
  )
    
  ad = pd.DataFrame(d)
  ad.head(2)

enter image description here

如何将以下代码行放入循环中以制作数据帧。我们从data得到两列,一个title,另一个row_di

row_di = dfc2.loc[dfc2.index== corpus[idx]].to_dict()
print("date:  " , row_di["date"][corpus[idx]] , "\n")
print("Title:  " , row_di["title"][corpus[idx]] , "\n")

0 个答案:

没有答案