基本上,我有一堆按颜色(项目)分类的独特项目的单一列表。我做了一些事情并使用这些独特项目(组合)的选定组合生成一个数据框。我的目标是列出原始列表中未出现在组合数据框中的项目。理想情况下,我想检查所有四个颜色列,但对于我的初始测试,我只选择了“红色”列。
import pandas as pd
Items = pd.DataFrame({'Id': ["6917529336306454104","6917529268375577150","6917529175831101427","6917529351156928903","6917529249201580539","6917529246740186376","6917529286870790429","6917529212665335174","6917529206310658443","6917529207434353786","6917529309798817021","6917529352287607192","6917529268327711171","6917529316674574229"
],'Type': ['Red','Blue','Green','Cyan','Red','Blue','Blue','Blue','Blue','Green','Green','Green','Cyan','Cyan']})
Items = Items.set_index('Id', drop=True)
#Do stuff
Combinations = pd.DataFrame({
'Red': ["6917529336306454104","6917529336306454104","6917529336306454104","6917529336306454104"],
'Blue': ["6917529268375577150","6917529286870790429","6917529206310658443","6917529206310658443"],
'Green': ["6917529175831101427","6917529207434353786","6917529309798817021","6917529309798817021"],
'Cyan': ["6917529351156928903","6917529268327711171","6917529351156928903","6917529268327711171"],
'Other': [12,15,18,32]
})
我的第一次尝试是使用下面的行,但这会引发执行错误“KeyError:'Id'”。一个论坛帖子表明可能会解决它,但这drop=True
在set_index
我的情况下似乎不起作用。
UnusedItems = ~Items[Items['Id'].isin(list(Combinations['Red']))]
我试图通过使用这条线来解决它。在执行时,它会生成一个空数据框。仅通过检查,在考虑“红色”列时应返回项目 6917529249201580539。考虑到所有组合列,项目 6917529249201580539、6917529246740186376、6917529212665335174 和 6917529316674574229 应作为未使用返回。
UnusedItems = ~Items[Items.iloc[:,0].isin(list(Combinations['Red']))]
我会很感激和想法或指导。谢谢。