对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设计】。

相关文章

django模型中的字段和model名显示为中文小技巧分享

简单方法: models.py 复制代码 代码如下: class IceCreamBar(models.Model):     title =  ...

python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别

python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别

前言 写爬虫有一个绕不过去的问题就是验证码,现在验证码分类大概有4种: 图像类 滑动类 点击类 语音类 今天先来看看图像类,这类验证码大多是数字、字母的组合,国内也...

python筛选出两个文件中重复行的方法

本文实例为大家分享了python脚本筛选出两个文件中重复的行数,供大家参考,具体内容如下 ''' 查找A文件中,与B文件中内容不重复的内容 ''' #!usr/bin/python...

TensorFlow 合并/连接数组的方法

如下所示: import tensorflow as tf a = tf.Variable([4,5,6]) b = tf.Variable([1,2,3]) c = tf.co...

用python求一重积分和二重积分的例子

首先是对一元函数求积分,使用Scipy下的integrate函数: from scipy import integrate def g(x): return (1-x**2)**...