compare 2 columns in different dataframes

徘徊边缘 提交于 2021-02-11 14:09:36

问题


if I have 2 tables b ,c and I want to go for c and check column called parent if the value equal to the value of column PrentSKU in table b, then bring the value of column ChildSKU from table b and put it in column Style in table c, else do nothing

I tried to use compare methods but the table size is different so it gave me an error, and I also tried for loop but it also gave me an error that The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

The code I have tried:

 for row in c['ParentSKU']:
        if row == b['Parent']:
           b['Style'] == c['ChildSKU']
        else: break  

回答1:


You can try something like this:

b = pd.DataFrame({'Parent':['a','b','c','d','e'],'ChildSKU':range(5)})

    Parent  ChildSKU
0   a   0
1   b   1
2   c   2
3   d   3
4   e   4

c = pd.DataFrame({'ParentSKU':['a','c','e']})

    ParentSKU   
0   a   
1   c   
2   e   

c['Style'] = c.merge(b,left_on='ParentSKU',right_on='Parent')['ChildSKU']

    ParentSKU   Style
0   a   0
1   c   2
2   e   4


来源:https://stackoverflow.com/questions/60586890/compare-2-columns-in-different-dataframes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!