我有两个数据 Df1
和 Df2
。我的 Df1['Col2]
中有几行是空的。我想用 Df2
的相应值填充此空白行。
Df1
Col1 Col2
1 AA
2
2
2
3 AC
3 AC
Df2
Cluster label
1 AA
2 AB
3 AC
4 AD
Desired Output
Col1 Col2
1 AA
2 AB
2 AB
2 AB
3 AC
3 AC
我正在尝试下面的代码,但没有得到结果:
Df1['Col2'] =np.where((Df2['Cluster']==Df1['Col1']),Df2['label'],'No label found')
我不能使用 merge
函数,因为我还有一些其他限制。
最佳答案
你可以在 col2 上结合应用和条件
df1{"col2"] = df1.apply(lambda x: df2[df2['Cluster'] == x ['col1']]['label'].tolist()[0] if x['col2'] is None else x['col2'], axis = 1)
关于python - 匹配两个dataframe中的两列,如果匹配得到对应的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63070207/