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')
相关推荐
jzlixiao 2020-09-15
cjsyrwt 2020-08-14
三石 2020-10-30
roamer 2020-10-29
三石 2020-10-29
wangquannuaa 2020-10-15
wangquannuaa 2020-09-29
wangquannuaa 2020-08-30
三石 2020-08-23
逍遥友 2020-08-21
jzlixiao 2020-08-18
wangquannuaa 2020-08-17
QianYanDai 2020-08-16
jzlixiao 2020-07-29
xirongxudlut 2020-07-20
mmmjyjy 2020-07-16
QianYanDai 2020-07-05
QianYanDai 2020-07-05
june0 2020-07-04