我有一个数据框df:
df
data = {'id':[12,112], 'idlist':[[1,5,7,12,112],[5,7,12,111,113]] } df=pd.DataFrame.from_dict(data)
看起来像这样:
id idlist 0 12 [1, 5, 7, 12, 112] 1 112 [5, 7, 12, 111, 113]
我需要检查是否id在中idlist,然后选择或标记它。我尝试了以下变化,并收到注释的错误:
id
idlist
df=df.loc[df.id.isin(df.idlist),:] #TypeError: unhashable type: 'list' df['flag']=df.where(df.idlist.isin(df.idlist),1,0) #TypeError: unhashable type: 'list'
一些可能的其他解决方法将.apply在列表理解中?
.apply
我在这里寻找一个解决方案,要么选择其中的行id为idlist,或标志为1,其中排id在idlist。结果df应为:
id idlist 0 12 [1, 5, 7, 12, 112]
要么:
flag id idlist 0 1 12 [1, 5, 7, 12, 112] 1 0 112 [5, 7, 12, 111, 113]
谢谢您的帮助!
用途apply:
apply
df['flag'] = df.apply(lambda x: int(x['id'] in x['idlist']), axis=1) print (df) id idlist flag 0 12 [1, 5, 7, 12, 112] 1 1 112 [5, 7, 12, 111, 113] 0
类似:
df['flag'] = df.apply(lambda x: x['id'] in x['idlist'], axis=1).astype(int) print (df) id idlist flag 0 12 [1, 5, 7, 12, 112] 1 1 112 [5, 7, 12, 111, 113] 0
与list comprehension:
list comprehension
df['flag'] = [int(x[0] in x[1]) for x in df[['id', 'idlist']].values.tolist()] print (df) id idlist flag 0 12 [1, 5, 7, 12, 112] 1 1 112 [5, 7, 12, 111, 113] 0
过滤解决方案:
df = df[df.apply(lambda x: x['id'] in x['idlist'], axis=1)] print (df) id idlist 0 12 [1, 5, 7, 12, 112] df = df[[x[0] in x[1] for x in df[['id', 'idlist']].values.tolist()]] print (df) id idlist 0 12 [1, 5, 7, 12, 112]