比较两个不同数据帧的“name”列

2024-04-26 22:03:13 发布

您现在位置:Python中文网/ 问答频道 /正文

actual_df['new_col'] = np.where(lookup_df['name'].str.contains(actual_df['name'] + '-'), lookup_df['name'], 'Not Found')

上述代码引发以下错误:

'Series' objects are mutable, thus they cannot be hashed

如何使用另一个数据帧执行所需的查找


Tags: 代码namedfnew错误npnotcol
1条回答
网友
1楼 · 发布于 2024-04-26 22:03:13

若两列的长度相同,则使用切片代替“str.contains()”方法

actual_df['new_col']= np.where(lookup_df['name'] == actual_df["name"].str[:-1], df2['name2'], 'Not Found')

如果没有,请使用下面的“df.isin()”方法:

actual_df['new_col'] = actual_df["name"][actual_df["name"].isin(lookup_df["name"].str[:-1])]

相关问题 更多 >