我有不同大小的列表(AAA,BBB,CBC)。我想比较列表并记录哪些列表具有相似的值。例如:
AAA BBB CBC
--- --- ---
A01 A01 A01
B02 C03 B02
C03 F06 D04
E05 F06
G07
我要找的结果看起来像这样
A01 = AAA, BBB, CBC
B02 = AAA, CBC
C03 = AAA, BBB, CBC
D04 = CBC
E05 = AAA
F06 = BBB, CBC
G07 = AAA
如果没有循环遍历列表并进行比较,有没有办法做到这一点?我尝试使用“isin”命令但没有成功。谢谢。
答案 0 :(得分:2)
让我们使用以下方法:
AAA = ['A01','B02','C03','E05','G07']
BBB = ['A01','C03','F06']
CBC = ['A01','B02','D04','F06']
lists = ['AAA','BBB','CBC']
df_out = pd.concat([pd.Series(eval(i)) for i in lists], axis=1, keys=lists)
df_out.stack().reset_index(name='value').groupby('value')['level_1'].apply(', '.join)
输出:
value
A01 AAA, BBB, CBC
B02 AAA, CBC
C03 BBB, AAA
D04 CBC
E05 AAA
F06 BBB, CBC
G07 AAA
Name: level_1, dtype: object