我有file1
,有6列,如下所示:
proNum proName color path comNum comName
1 apple red ewtt 43 namex
1 apple red lfor 27 namey
1 apple red bcms 276 namez
4 orange yellow owif 7 namea
4 orange yellow zxaq 19 nameb
8 burry bink lpoq 99 namec
和file2
,两列如下:
comName color
nameb red
namex bink
nameb black
namez blue
namec yellow
namey red
namez red
nameb bink
我要找的是在file1
中添加一列,结果是/from:check/go遍历file1
中comName
的每个值,将它与file2
中的comName
逐一进行比较,如果找到相同的值,则取file2
中color
的对应值,并与file1
中的color
进行比较,如果相同,则将color
的值放入该新列并退出,然后检查comName
的下一个值
如果color
的值不相等,则继续在file2
中进行,因为comName
对其中一些值显示一个或多个值,如果在file2
中检查finish并且在color
中没有相等的值,则在新列中输入No/Nan
以下是输出:
proNum proName color path comNum comName New_color
1 apple red ewtt 43 namex No
1 apple red lfor 27 namey red
1 apple red bcms 276 namez red
4 orange yellow owif 7 namea No
4 orange yellow zxaq 19 nameb No
8 burry bink lpoq 99 namec No
这似乎很容易,但这些混音比较真的困惑我怎么做,所以任何帮助/想法将不胜感激
在comName和color列上的表上应用左联接应使leed达到所需的输出
file2['color_y']=['red','bink','black','blue','yellow','red','red','bink']
file1.merge(file2, how='left', on=['comName','color'].fillna('No')
请尝试以下代码:
df
是file1
,df2
是file2
输出:
您可以在这里使用合并,这样您只能在
comName
和color
上完全匹配时分配New_color
输出
相关问题 更多 >
编程相关推荐