Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据帧列中存在的元素,列为列表格式,其他列只有一个元素(关系查询)_Python_Pandas - Fatal编程技术网

Python 数据帧列中存在的元素,列为列表格式,其他列只有一个元素(关系查询)

Python 数据帧列中存在的元素,列为列表格式,其他列只有一个元素(关系查询),python,pandas,Python,Pandas,TypeError:(“int”类型的参数不可iterable”,“在索引0处出现”) 数据帧输出原稿 懒汉 2 [2, 33] 3 [3, 3] 4 [4, 73] 5 [5, 20] 6 [6, 41] 7 [7, 41, 73, 44, 10, 32, 11, 31, 20, 74, 17, 69, 8, 16, 23, 43, 72, 70, 7] 9 [9, 9] 10 [10, 10, 68] ... 20,20,21,36,40 需要在id列中找到id

TypeError:(“int”类型的参数不可iterable”,“在索引0处出现”)

数据帧输出原稿 懒汉 2 [2, 33] 3 [3, 3] 4 [4, 73] 5 [5, 20] 6 [6, 41] 7 [7, 41, 73, 44, 10, 32, 11, 31, 20, 74, 17, 69, 8, 16, 23, 43, 72, 70, 7] 9 [9, 9] 10 [10, 10, 68] ... 20,20,21,36,40

需要在id列中找到idlist元素

例如,第5行中的20出现在第20行中

编辑数据帧 数据={ “id”:[2,3,4,5,6,7,9,10,11,12,13,14,17,18,19,20,21,22,23,24], “游手好闲者”:[33],[3],[73],[20],[41,73,44,10,32,11,31,20,74,17,69,8,16,23,43,72,70],[9],[68],[111],[24],[11,20],[2,20],[3,68],[188],[33],[21,36,40],[3],[41,3],[56],[33]]

data = {
        'id' :[2,3,4,5,6,7,9,10,11,12,13,14,17,18,19,20,21,22,23,24],
        'idlist': [[2,33],[3,3],[4,73],[5,20],[6,41],[7,41,73,44,10,32,11,31,20,74,17,69,8,16,23,43,72,70,7],[9,9],[10,10,68],[11,11,11],[12,24],[13,20],[14,20],[17,17,68],[18,18],[19,33],[20,20,21,36,40],[21,21],[22,41],[23,23,56,],[24,33,24,]]

        }

df=pd.DataFrame.from_dict(data)
df['flag'] = df.apply(lambda x: int(x['id'] in x['idlist']), axis=1)

df['flag'] = df.apply(lambda x: int( x['idlist'] in x['id']), axis=1) --- error when i try to  find a list of elements in the idlist to the id column...
[参考此链接][1] 耶斯雷尔-回答并尝试另一种方法,但出现了错误

    }
TypeError:(“int”类型的参数不可iterable”,“在索引0处出现”)

输出数据帧

df['flag'] = df.apply(lambda x: int( x['idlist'] in x['id']), axis=1)

这是你想要的吗

   id   idlist  foundlist
2   [33]    
4   [73]    
5   [20]    [20]
6   [41]    
7   [41, 73, 44, 10, 32, 11, 31, 20, 74, 17, 69, 8, 16, 23, 43, 72, 70] [10,11,20,17]
10  [68]    
11  [111]   
12  [24]    
13  [11, 20]    [11]
14  [2, 20] [2,20]
17  [3, 68] [3]
18  [188]   
19  [33]    
20  [21, 36, 40]    

这是你想要的吗

   id   idlist  foundlist
2   [33]    
4   [73]    
5   [20]    [20]
6   [41]    
7   [41, 73, 44, 10, 32, 11, 31, 20, 74, 17, 69, 8, 16, 23, 43, 72, 70] [10,11,20,17]
10  [68]    
11  [111]   
12  [24]    
13  [11, 20]    [11]
14  [2, 20] [2,20]
17  [3, 68] [3]
18  [188]   
19  [33]    
20  [21, 36, 40]    
如果你想查一下

如果同一行的“id”列中存在de'idlist'列中的int

这和另一条路是一样的

df['flag'] = df.apply(lambda x: int( x['idlist'] in x['id']), axis=1) --- error when i try to  find a list of elements in the idlist to the id column..
第一个选项给您一个错误,因为x['id']返回一个int,而不是像x['idlist']这样的列表对象。int是不可编辑的

如果要根据整个列id检查行的任何x['idlist'],可以

df['flag'] = df.apply(lambda x: int(x['id'] in x['idlist']), axis=1)
看到差异了吗?您根据整个列id(df)计算x(行级别)

如果你想查一下

如果同一行的“id”列中存在de'idlist'列中的int

这和另一条路是一样的

df['flag'] = df.apply(lambda x: int( x['idlist'] in x['id']), axis=1) --- error when i try to  find a list of elements in the idlist to the id column..
第一个选项给您一个错误,因为x['id']返回一个int,而不是像x['idlist']这样的列表对象。int是不可编辑的

如果要根据整个列id检查行的任何x['idlist'],可以

df['flag'] = df.apply(lambda x: int(x['id'] in x['idlist']), axis=1)


看到区别了吗?您(在行级别上)对整个列id(df)求值x

为了让我正确,您想检查每个idlist中的每个id是否都在id列中?是的,这是正确的TypeError:类型为“int”的参数不是Iterablen不确定是否理解,您的解决方案意味着查找if
[2,33]
2
中,所以没有意义。你能解释更多吗,你需要什么?我编辑了数据帧idlist只是为了让它正确,你想检查每个idlist中的每个id是否都在id列中吗?是的,这是正确的TypeError:类型为“int”的参数不是Iterablen不确定是否理解,你的解决方案意味着查找
[2,33]
位于
2
中,因此没有任何意义。您能进一步解释一下吗,您需要什么?我已经编辑了数据帧IDLIST我可以在另一列中标记1后获得元素编号/名称吗(当前元素的名称)我不确定我是否理解您的问题。您希望元素本身还是列表中的索引位置?如果您还可以编写有助于lotI的预期输出,我希望元素本身,(值)如果我的idlist没有[]列表或方括号,或者如何在dataframedf['col'中的idlist列中添加方[]呢='str'+df['col'].astype(str)我能在另一列中标记1后获得元素编号/名称吗,(当前元素的名称)我不确定我是否理解您的问题。您想要元素本身还是列表中的索引位置?如果您还可以编写有助于用户的预期输出,我想要元素本身,(值)如果我的idlist没有[]列表或方括号,或者如何在dataframedf['col']='str'+df['col'].astype(str)中向idlist列添加方[]。我还需要id列表中的标志id df['idlist']=df['idlist'].str.replace(','').str split()df['label']=df['label'].astype(str)如果您的字符串有问题,这可能会起作用。我还需要id列表中的标志id df['idlist']=df['idlist'].str.replace(',','').str.split()df['label']=df['label'].astype(str)如果您的字符串有问题,这可能会起作用