Pandas 同元素多列去重的实例
作者:迪小子 发布时间:2023-02-09 21:03:32
标签:Pandas,多列,去重
有一些问题可能会遇到同元素多列去重问题,下面介绍一种非常简单效率也很快的做法,用pandas来实现。
首先我们看一下数据类型:
G1 G2
a b
b a
c d
d c
e f
对这样的两列数据进行同元素去重,最终得到结果为:
G1 G2
a b
c d
e f
代码如下:
#-*- coding: utf-8 -*-
data = {'G1':['a','b','c','d','e'],'G2':['b','a','d','c','f']}
data = pd.DataFrame(data)
data['G3'] = data['G1'] + '|' + data['G2']
p = []
for i in data['G3'].tolist():
tmp = sorted(i.split('|')) # The most important part,sort
p.append(tmp[0] + '|' + tmp[1])
data['G3'] = pd.Series(p)
data = data.drop_duplicates('G3')
来源:https://blog.csdn.net/u011476718/article/details/62416776
0
投稿
猜你喜欢
- 前言最近在写一个移动端的地图项目,也是首次完整的去了解百度地图api,这篇博客会手把手的教你如何使用百度地图api和一些常见问题,后续我也会
- 定义和用法strftime() 函数根据区域设置格式化本地时间/日期。语法strftime(format,timestamp)参数 描述 f
- 问题:想装tfx,但是提示不支持python3.9。解决方案:新建一个环境tfx专门用来运行流水线,这个环境安装python3.8。1.查看
- 说明eventlet是一个必备工具,经常用,绿化原理有点忘记了,重新复习一遍.三个主要问题 1. 绿化的原理是什么?2. 绿化怎么
- 下面我给出几种常用的方法: 1 .对象冒充 原理: 构造函数使用this关键字给所有属性和方法赋值, 因为构造函数只是一个函数,所以可以使C
- 一.字典的基本方法1.新建字典1)、建立一个空的字典>>> dict1={} >>> dict2=dic
- SQLServer数据库从高版本降级到低版本实例详解由于目前还广泛使用着SQLServer2000,很多公司又想使用新的SQLServer,
- Python的matplotlib模块中的errorbar函数可以绘制误差棒图,本次主要绘制不带折线的误差棒图。1.基本参数errorbar
- PyCharm 具备一般 IDE 的功能,比如,调试、语法高亮、项目管理、代码跳转、智能提示、自动完成、单元测试、版本控制…另外,PyCha
- 1、图片防盗链在一些大型网站中,比如百度贴吧,该站点的图片采用了防盗链的规则,以至于使用下面代码会发生错误。简单代码:<!DOCTYP
- input框点击后不聚焦问题废话不多说直接上代码哪个地方要写input框 就直接把这一部分代码放上 里面双向绑定的
- 本文实例讲述了Python实现读写sqlite3数据库并将统计数据写入Excel的方法。分享给大家供大家参考,具体如下: src
- 这篇文章主要介绍了Python如何使用Gitlab API实现批量的合并分支,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- drop方法有一个可选参数inplace,表明可对原数组作出修改并返回一个新数组。不管参数默认为False还是设置为True,原数组的内存值
- 我们想要知道数目的总和,只要通过+就能实现,这是我们在做题上经常用到的符号。但是在python中不能直接使用,我们需要借助一些代码或者函数帮
- DBCC CHECKIDENT(N'dbo.Orders', RESEED, 0); DBCC CHECKIDENT 语法
- 一、分析网页1. 打开网页,在搜索框输入百度翻译并进入百度翻译网站中。F12调出开发者工具,点击Network(网络)\ Fetch/XHR
- 具体不多说,直接上代码。欢迎一起交流和学习。创建一个按月创建表的存储过程,SQL语句如下:DELIMITER // DROP PROCEDU
- Python字符串的format函数format()函数用来收集其后的位置参数和关键字段参数,并用他们的值填充字符串中的占位符。通常格式如下
- actions异步修改状态与mutations同步修改状态是两个容易混淆的概念,因为两者在执行上,很难测试出两者的差别,而我们要区别它们两,