2017-03-16 156 views
1

需要一些帮助,让uniqued值从熊猫数据框中熊猫数据框中唯一值

我有:

>>> df1 
    source target metric 
0 acc1.yyy acx1.xxx 10000 
1 acx1.xxx acc1.yyy 10000 

的目标是消除基于源+目标或目标+源唯一值。但我不能用drop_duplicates获得这个。

>>> df2 = df1.drop_duplicates(subset=['source','target']) 
>>> df2 
    source target metric 
0 acc1.yyy acx1.xxx 10000 
1 acx1.xxx acc1.yyy 10000 

[更新]

也许重复的不正确的单词在这里,所以让我上面解释进一步

id source target 
0 bng1.xxx.00 bdr2.xxx.00 
1 bng1.xxx.00 bdr1.xxx.00 
2 bdr3.yyy.00 bdr3.xxx.00 
3 bdr3.xxx.00 bdr3.yyy.00 
4 bdr2.xxx.00 bng1.xxx.00 
5 bdr1.xxx.00 bng1.xxx.00 

,我想删除拥有为例源=目标entryies和target = source。

0 and 4 = same pair 
1 and 5 = same pair 
2 and 3 = same pair 

end goal will be to keep 0 1 2 or 4 5 3 . 
+1

不明白你想要做什么。请澄清“基于源+目标或目标+源删除唯一值”。输入和输出的例子会有所帮助。 – Denziloe

+0

我需要采用acc1.yyy + acx1.xxx对,并确保没有与匹配acx1.xxx + acc1.yyy对的OR匹配的条目。 – Cmarv

+0

公制列的情况如何?如果有重复,应使用哪个值?再次,请编辑您的问题以包含示例输入和您想要的输出。 – Denziloe

回答

1

你需要在前排序两列:

df1[['source','target']] = df1[['source','target']].apply(sorted,axis=1) 
print (df1) 
    source target metric 
0 acc1.yyy acx1.xxx 10000 
1 acc1.yyy acx1.xxx 10000 

df2 = df1.drop_duplicates(subset=['source','target']) 
print (df2) 
    source target metric 
0 acc1.yyy acx1.xxx 10000 

编辑:

看来列source需要被改变 - 删除最后3个字符:

df1['source1'] = df1.source.str[:-3] 
df1[['source1','target']] = df1[['source1','target']].apply(sorted,axis=1) 
print (df1) 
    id   source  target  source1 
0 0 bng1.xxx.00-00 bng1.xxx.00 bdr2.xxx.00 
1 1 bng1.xxx.00-00 bng1.xxx.00 bdr1.xxx.00 
2 2 bdr3.yyy.00-00 bdr3.yyy.00 bdr3.xxx.00 
3 3 bdr3.xxx.00-00 bdr3.yyy.00 bdr3.xxx.00 
4 4 bdr2.xxx.00-00 bng1.xxx.00 bdr2.xxx.00 
5 5 bdr1.xxx.00-00 bng1.xxx.00 bdr1.xxx.00 

df2 = df1.drop_duplicates(subset=['source1','target']) 
df2 = df2.drop('source1', axis=1) 
print (df2) 
    id   source  target 
0 0 bng1.xxx.00-00 bng1.xxx.00 
1 1 bng1.xxx.00-00 bng1.xxx.00 
2 2 bdr3.yyy.00-00 bdr3.yyy.00 
+0

不适合我。我已更新该帖子以反映我期望实现的目标 – Cmarv

+0

,请检查已编辑的答案。 – jezrael

+0

thx。作品!!!! – Cmarv

0

你的定义重复和熊猫使用的不一样。在熊猫中,如果相应的条目相同,则两行被认为是重复的。在下面的例子中,行1和行2不重复,因为它们对于对应的变量具有不同的值,而行3和4是重复的。

df = {'source':['acc1.yyy', 'acx1.xxx', 'acc1.xxx', 'acc1.xxx'], 'target': ['acx1.xxx', 'acc1.yyy', 'acc1.yyy', 'acc1.yyy']} 
df = pd.DataFrame(df) 
df 
    # source target 
# 0 acc1.yyy acx1.xxx 
# 1 acx1.xxx acc1.yyy 
# 2 acc1.xxx acc1.yyy 
# 3 acc1.xxx acc1.yyy 
df.drop_duplicates() 
    # source target 
# 0 acc1.yyy acx1.xxx 
# 1 acx1.xxx acc1.yyy 
# 2 acc1.xxx acc1.yyy 

对于您提到的情况,请创建一个新列,它是源列和目标列的元组。请尝试以下方法

df.loc[:, 'src_tgt'] = pd.Series([tuple(sorted(each)) for each in list(zip(df.source.values.tolist(), df.target.values.tolist()))]) 
df 
    # source target    src_tgt 
# 0 acc1.yyy acx1.xxx (acc1.yyy, acx1.xxx) 
# 1 acx1.xxx acc1.yyy (acx1.xxx, acc1.yyy) 
# 2 acc1.xxx acc1.yyy (acc1.xxx, acc1.yyy) 
# 3 acc1.xxx acc1.yyy (acc1.xxx, acc1.yyy) 
df.drop_duplicates(subset=['src_tgt']) 
    # source target    src_tgt 
# 0 acc1.yyy acx1.xxx (acc1.yyy, acx1.xxx) 
# 2 acc1.xxx acc1.yyy (acc1.xxx, acc1.yyy) 
+0

不适合我。我已更新帖子以反映我期望实现的目标 – Cmarv