对dataframe数据之间求补集的实例详解

yipeiwu_com6年前Python基础

python的pandas库,对于dataframe数据,有merge命令可以完成dataframe数据之间的求取交集并集等命令。

若存在df1与df2 ,他们的交集df3=pd.merge(df1,df2,on=[.....])。但是又想通过df3求df3与df1的补集时发现没有该命令。

求df3(子集)与df1补集:

#x为子集

def Complement(x,y):

 import numpy as np

 array1 = np.array(x)

 list1=array1.tolist()

 

 array2=np.array(y)

 list2=array2.tolist()

 

 def list_to_tuple(t):

  l = []

  for e in t:

   l.append(tuple(e))

  return l

 

 def tuple_to_list(t):

  l = []

  for e in t:

   l.append(list(e))

  return l

 

 a=list_to_tuple(list1)

 b=list_to_tuple(list2)

 set3=set(b).difference(set(a))

 list3=list(set3)

 list4=tuple_to_list(list3)

 

 from pandas import Series,DataFrame

 df1=DataFrame(list4,columns=x.columns)

 

 return df1

以上这篇对dataframe数据之间求补集的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中线程编程之threading模块的使用详解

threading.Thread Thread 是threading模块中最重要的类之一,可以使用它来创建线程。有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法;另...

python 多线程应用介绍

python可以方便地支持多线程。可以快速创建线程、互斥锁、信号量等等元素,支持线程读写同步互斥。美中不足的是,python的运行在python 虚拟机上,创建的多线程可能是虚拟的线程,...

linux 下实现python多版本安装实践

使用pythonbrew 复制代码 代码如下: easy_install pythonbrew [root@li637-23 schirm]# pythonbrew_install We...

python计算最大优先级队列实例

复制代码 代码如下:# -*- coding: utf-8 -*- class Heap(object):     @classmethod &n...

pytorch 数据处理:定义自己的数据集合实例

数据处理 版本1 #数据处理 import os import torch from torch.utils import data from PIL import Image im...