考虑列而不是字符串

时间:2020-09-08 12:55:31

标签: python pandas numpy

我正在尝试将以下代码应用于此列:

Test
"Find the number behind the lines"
"Look at the sky"
"It is a such wonderful day today"

在以下代码中,docs是文档列表;就我而言,它们应该是“测试”列中的行。

D = np.zeros((len(docs), len(docs)))
for i in range(len(docs)):
    for j in range(len(docs)):
        if i == j:
            continue  
        if i > j:
            D[i, j] = D[j, i] 

如何将其应用于我的专栏?

在我的代码中,我假设您的字符串/行列表(每个单词列表)是docs,以使用上述代码计算成对距离D的数组。问题在于将其应用于列。

期望的输出(但不幸的是,我无法用上面的代码确定)将是参考句子和其他句子的相似性。 i,j是我的索引,它们遍历Test列中的每一行。我要使用的算法是移动器的距离。

2 个答案:

答案 0 :(得分:0)

从您的问题中我了解到您想使用文档字符串重命名您的行和列。如果是这样,请尝试

docs=["Find the number behind the lines","Look at the sky","It is a such wonderful day today"]

D = np.zeros((len(docs), len(docs)))
df=pd.DataFrame(D,columns=docs,index=docs)
print(df)

答案 1 :(得分:0)

def f(docs):

D = np.zeros((len(docs), len(docs)))
for i in range(len(docs)):
    for j in range(len(docs)):
        if i == j:
            continue  
        if i > j:
            D[i, j] = D[j, i] 




df.Test.apply(lambda x: f(x))