pandas 中的重复行追加到循环中
duplicated rows in pandas append inside for loop
我在函数内使用 for 循环时遇到问题。我正在计算单词向量列表的余弦距离。对于每个向量,我正在计算余弦距离,然后将其作为新列附加到 pandas 数据帧。问题是有几个模型,所以我将模型 1 中的词向量与其他所有模型中的词向量进行比较。
这意味着有些词并非在所有模型中都存在。在这种情况下,我为 KeyError 使用了一个异常,并允许循环继续进行而不抛出错误。如果发生这种情况,我还要求在 pandas 数据帧中添加一个 0 值。这导致了重复的索引,并且无法从这里继续前进。代码如下:
from scipy.spatial.distance import cosine
import pandas as pd
def cosines(model1, model2, model3, model4, model5, model6, model7, words):
df = pd.DataFrame()
model = [model2, model3, model4, model5, model6, model7]
for i in model:
for j in words:
try:
cos = 1 - cosine(model1.wv[j], i.wv[j])
print(f'cosine for model1 vs {i.name:} {1 - cosine(model1[j], i[j])}')
tempdf = pd.DataFrame([cos], columns=[f'{j}'], index=[f'{i.name}'])
#print(tempdf)
df = pd.concat([df, tempdf], axis=0)
except KeyError:
print(word not present at {i.name}')
ke_tempdf = pd.DataFrame([0], columns=[f'{j}'], index=[f'{i.name}'])
df = pd.concat([df, ke_tempdf], axis=0)
pass
return df
然而,该函数适用于每个 KeyError - 它不是在一行中添加 0,而是创建一个值为 0 的新重复行。用两个词复制了数据帧,但最终目的是让许多单词的列表。生成的数据框如下所示:
word1 word2
model1 0.000000 NaN
model1 NaN 0.761573
model2 0.000000 NaN
model2 NaN 0.000000
model3 0.000000 NaN
model3 NaN 0.000000
model4 0.245140 NaN
model4 NaN 0.680306
model5 0.090268 NaN
model5 NaN 0.662234
model6 0.000000 NaN
model6 NaN 0.709828
如您所见,对于每个不存在的单词,不是向现有模型行 (NaN) 添加 0,而是添加一个带有数字 0 的新行。它应该显示为:model1, 0, 0.76
等等,而不是重复的行。非常感谢任何帮助,谢谢!
没有你的模型对象我无法完全测试它,但我认为这可以解决你的问题:
from scipy.spatial.distance import cosine
import pandas as pd
def cosines(model1, model2, model3, model4, model5, model6, model7, words):
df = pd.DataFrame()
model = [model2, model3, model4, model5, model6, model7]
for i in model:
cos_dict = {}
for j in words:
try:
cos_dict[j] = 1 - cosine(model1.wv[j], i.wv[j])
print(f'cosine for model1 vs {i.name:} {1 - cosine(model1[j], i[j])}')
except KeyError:
print(f'word not present at {i.name}')
cos_dict[j] = 0
tempdf = pd.DataFrame.from_dict(cos_dict, orient='columns')
tempdf.index = [f'{i.name}']
df = pd.concat([df, tempdf])
return df
它在内循环中的字典中收集每个模型的单词值,并且仅在外循环中将它们添加到完整数据帧中一次。
我在函数内使用 for 循环时遇到问题。我正在计算单词向量列表的余弦距离。对于每个向量,我正在计算余弦距离,然后将其作为新列附加到 pandas 数据帧。问题是有几个模型,所以我将模型 1 中的词向量与其他所有模型中的词向量进行比较。
这意味着有些词并非在所有模型中都存在。在这种情况下,我为 KeyError 使用了一个异常,并允许循环继续进行而不抛出错误。如果发生这种情况,我还要求在 pandas 数据帧中添加一个 0 值。这导致了重复的索引,并且无法从这里继续前进。代码如下:
from scipy.spatial.distance import cosine
import pandas as pd
def cosines(model1, model2, model3, model4, model5, model6, model7, words):
df = pd.DataFrame()
model = [model2, model3, model4, model5, model6, model7]
for i in model:
for j in words:
try:
cos = 1 - cosine(model1.wv[j], i.wv[j])
print(f'cosine for model1 vs {i.name:} {1 - cosine(model1[j], i[j])}')
tempdf = pd.DataFrame([cos], columns=[f'{j}'], index=[f'{i.name}'])
#print(tempdf)
df = pd.concat([df, tempdf], axis=0)
except KeyError:
print(word not present at {i.name}')
ke_tempdf = pd.DataFrame([0], columns=[f'{j}'], index=[f'{i.name}'])
df = pd.concat([df, ke_tempdf], axis=0)
pass
return df
然而,该函数适用于每个 KeyError - 它不是在一行中添加 0,而是创建一个值为 0 的新重复行。用两个词复制了数据帧,但最终目的是让许多单词的列表。生成的数据框如下所示:
word1 word2
model1 0.000000 NaN
model1 NaN 0.761573
model2 0.000000 NaN
model2 NaN 0.000000
model3 0.000000 NaN
model3 NaN 0.000000
model4 0.245140 NaN
model4 NaN 0.680306
model5 0.090268 NaN
model5 NaN 0.662234
model6 0.000000 NaN
model6 NaN 0.709828
如您所见,对于每个不存在的单词,不是向现有模型行 (NaN) 添加 0,而是添加一个带有数字 0 的新行。它应该显示为:model1, 0, 0.76
等等,而不是重复的行。非常感谢任何帮助,谢谢!
没有你的模型对象我无法完全测试它,但我认为这可以解决你的问题:
from scipy.spatial.distance import cosine
import pandas as pd
def cosines(model1, model2, model3, model4, model5, model6, model7, words):
df = pd.DataFrame()
model = [model2, model3, model4, model5, model6, model7]
for i in model:
cos_dict = {}
for j in words:
try:
cos_dict[j] = 1 - cosine(model1.wv[j], i.wv[j])
print(f'cosine for model1 vs {i.name:} {1 - cosine(model1[j], i[j])}')
except KeyError:
print(f'word not present at {i.name}')
cos_dict[j] = 0
tempdf = pd.DataFrame.from_dict(cos_dict, orient='columns')
tempdf.index = [f'{i.name}']
df = pd.concat([df, tempdf])
return df
它在内循环中的字典中收集每个模型的单词值,并且仅在外循环中将它们添加到完整数据帧中一次。