问题
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