You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

合并两个DataFrame时无法生成含正确值的新DataFrame求助

解决DataFrame合并出现NaN的问题

问题原因

你当前的代码是把三个Series作为行传入pd.DataFrame(),但每个Series只有9个元素,构造DataFrame时会导致大部分位置无对应值,从而出现NaN。

正确的合并方法

方法1:直接添加列到原DataFrame

由于两个DataFrame索引完全对齐(均为0-8),可直接将相似度列追加到第一个DataFrame:

combineddf = myquerycontents_and_embeddings_df.copy()
combineddf['cosine_similarity'] = similarityvaluedf['cosinesimilarity']

方法2:用pd.concat按列合并

利用concat函数指定按列(axis=1)合并两个DataFrame,若需要重命名列可额外处理:

import pandas as pd
combineddf = pd.concat([myquerycontents_and_embeddings_df, similarityvaluedf], axis=1)
# 可选:重命名列名
combineddf = combineddf.rename(columns={'cosinesimilarity': 'cosine_similarity'})

方法3:字典形式构造DataFrame

直接用字典组织列名与对应Series,传入pd.DataFrame:

combineddf = pd.DataFrame({
    'content': myquerycontents_and_embeddings_df['content'],
    'embeddings': myquerycontents_and_embeddings_df['embeddings'],
    'cosine_similarity': similarityvaluedf['cosinesimilarity']
})

验证结果

执行任意上述方法后,combineddf会包含3列完整数据,无NaN值,结构示例如下:

contentembeddingscosine_similarity
0i live in space[0.00169137..., -0.01332047..., ...]0.994341
1i live my life to fullest[-0.01872972..., -0.01036668..., ...]0.808836

内容的提问来源于stack exchange,提问作者Manu Chadha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 15:22:34