查找对称条目数据框,如果不删除条目 [英] Find symmetrical entries dataframe, if not delete entry

查看:48
本文介绍了查找对称条目数据框,如果不删除条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据.

ID1 ID2 Value
1    2   5.5
2    1    10
1    3    5

预期输出:

ID1 ID2 Value
1    2   5.5
2    1    10

当我有一个对称条目的值时,我只想保留数据.如果我只有一个条目,例如ID1 = 1和ID2 = 3,但没有ID1 = 3和ID2 = 1的条目,那么我想删除此数据行.我该怎么用熊猫呢?

I only want to hold data, when I have a value for the symmetrical entry. If I only have a entry e.g. with ID1=1 and ID2=3 but no entry for ID1=3 and ID2=1 then I want to delete this datarow. How can I do this with pandas?

推荐答案

如果列ID1ID2中成对的所有值都是唯一的,请首先使用np.sort创建帮助器DataFrame,并使用<返回所有重复的行a href ="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.duplicated.html" rel ="nofollow noreferrer"> DataFrame.duplicated :

If all values in pairs in columns ID1 and ID2 are unique first create helper DataFrame with np.sort and return all duplicated rows with DataFrame.duplicated:

df1 = pd.DataFrame(np.sort(df[['ID1','ID2']], axis=1), index=df.index)

df = df[df1.duplicated(keep=False)]
print (df)
   ID1  ID2  Value
0    1    2    5.5
1    2    1   10.0

这篇关于查找对称条目数据框,如果不删除条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆