我正在编写一个程序,其中一个函数创建一个对象,从该对象调用一个函数,然后使用输出,该输出应该是一个充满浮点数的Pandas DataFrame,如下所示:
def func_a():
text_df = None
ts = TextSearcher()
text_df = (ts.topic_sim(list(p1), False)
text_max = text_df.max().max()
#etc
class TextSearcher():
def topic_sim(p1, jstor=None):
doclist = []
df = pd.DataFrame(index=list(p1), columns=list(p1))
for i in range(len(p1)):
doclist.append(p1[i].attrs['title'])
for i in range(len(p1)-1):
for j in range(i+1,len(p1)):
score = self.get_BM25_score(p1[i].attrs['DOI'], doclist)
df.loc[p1[i].attrs['DOI'],p1[j].attrs['DOI']] = score
df.loc[p1[j].attrs['DOI'],p1[i].attrs['DOI']] = score
return df
当我告诉topic_sim在返回它之前立即打印(df)时,它会打印所需的输出:
10.1007/s00799-015-0156-0 ... 10.1002/anie.199013901
10.1007/s00799-015-0156-0 NaN ... 0.452615
10.1002/anie.198709311 0.452615 ... 1.79016
10.1002/anie.199013901 0.452615 ... 1.79016
10.1021/acscatal.7b00482 0.452615 ... 3.19946
10.1002/anie.198916551 0.452615 ... 1.55234
10.1002/anie.199013901 0.452615 ... 1.79016
但是,在func(a)中,df = topic_sim(p1, False)
是一个充满NaN的数据框,如下所示:
10.1007/s00799-015-0156-0 ... 10.1002/anie.199013901
10.1007/s00799-015-0156-0 NaN ... NaN
10.1002/anie.198709311 NaN ... NaN
10.1002/anie.199013901 NaN ... NaN
10.1021/acscatal.7b00482 NaN ... NaN
10.1002/anie.198916551 NaN ... NaN
10.1002/anie.199013901 NaN ... NaN
为什么会发生这种情况,如何使topic_sim中的df具有应返回的值?