[英]Slicing Pandas Dataframe based on a value present in a column which is a list of lists
[英]Need to create list using list value which are present in dataframe column
这是我的 dataframe:
prod_sheet:
Product ID
0 Prod1 00P000000000101
1 Prod2 00P000000000105
2 Prod3 00P000000000109
3 Prod4 00P000000000119
4 Prod5 00P000000000120
L=[Prod2,Prod4,Prod5]
列表中存在的产品的 ID
needed_list=[00P000000000105,00P000000000119,00P000000000120]
如果顺序是重要的列表理解,则使用:
L=['Prod5','Prod4','Prod3']
s = prod_sheet.set_index('Product')['ID']
needed_list = [s[p] for p in L]
print (needed_list)
['00P000000000120', '00P000000000119', '00P000000000109']
如果顺序不重要,请使用:
needed_list = prod_sheet.loc[prod_sheet['Product'].isin(L), 'ID'].tolist()
print (needed_list)
'00P000000000109', '00P000000000119', '00P000000000120']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.