Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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_Dataframe - Fatal编程技术网

python熊猫在列中获得不同的匹配

python熊猫在列中获得不同的匹配,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据帧,看起来有点像这段代码给出的: import pandas as pd data = {'check1': ['a', 'a', 'b', 'd', 'f', 'f', 'g'], 'check2': ['b', 'c', 'c', 'e', 'g', 'h', 'h']} df = pd.DataFrame (data, columns = ['check1','check2']) 我想要得到的是一个列表或数据帧或类似的东西的列表,它告诉我在两个方向上两列之间

我有一个数据帧,看起来有点像这段代码给出的:

import pandas as pd
data = {'check1':  ['a', 'a', 'b', 'd', 'f', 'f', 'g'],
        'check2': ['b', 'c', 'c', 'e', 'g', 'h', 'h']}
df = pd.DataFrame (data, columns = ['check1','check2'])
我想要得到的是一个列表或数据帧或类似的东西的列表,它告诉我在两个方向上两列之间的不同匹配。应该是这样的:

[['a', 'b', 'c'], ['d', 'e'], ['f', 'g', 'h']]
我尝试过这样做,但我无法让它同时兼顾所有比赛:

df.groupby('check1').apply(lambda x: x['check2'].unique()).apply(pd.Series).reset_index()
这是我来过的最接近的一次,但它似乎有点像黑客,并没有在两个方向都这样做,也没有删除任何重复项。我不知道是否有更合乎逻辑/优雅的方法。
星期二之前我不会再工作了,但在此之前如果有人有什么好主意,我将不胜感激。

试着把相同的子列表看作一个连接,这样更像是网络问题

import networkx as nx
G=nx.from_pandas_edgelist(df, 'check1', 'check2')
l=list(nx.connected_components(G))
l
Out[133]: [{'a', 'b', 'c'}, {'d', 'e'}, {'f', 'g', 'h'}]

啊,太好了。太感谢了,我要挣扎好长时间才能做到这一点!