根据两列的内容而不是顺序删除重复项 [英] Remove duplicates based on the content of two columns not the order

查看:41
本文介绍了根据两列的内容而不是顺序删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相关矩阵,已分解成一个数据框,因此现在有以下示例:

I have a correlation matrix that i melted into a dataframe so now i have the following for example:

First      Second       Value
A          B            0.5
B          A            0.5
A          C            0.2 

我只想删除前两行之一.怎么做呢?

i want to delete only one of the first two rows. What would be the way to do it?

推荐答案

使用:

#if want select columns by columns names
m = ~pd.DataFrame(np.sort(df[['First','Second']], axis=1)).duplicated()
#if want select columns by positons
#m = ~pd.DataFrame(np.sort(df.iloc[:,:2], axis=1)).duplicated()
print (m)

0     True
1    False
2     True
dtype: bool

df = df[m]
print (df)
  First Second  Value
0     A      B    0.5
2     A      C    0.2

这篇关于根据两列的内容而不是顺序删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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