对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脚本在Nginx和uwsgi上部署MoinMoin的教程

在 CentOS 下使用 apache+mod_wsgi 部署了 MoinMoin,但是编辑和保存页面很慢,于是准备使用 nginx+uwsgi 重新部署 本文假定已经按照官方指引 Qu...

Python中为feedparser设置超时时间避免堵塞

python有一个用于解析feed的模块:feedparser,feedparser解析各种feed是非常方便的,唯一比较恼火的是遇到一些badurl,经常会导致堵塞,因此需要为feed...

Python 实现淘宝秒杀的示例代码

新手学习Python,之前在网上看见一位朋友写的40行Python代码搞定京东秒杀,想在淘宝上帮女朋友抢玩偶,所以就照猫画虎的写了下淘宝的秒杀脚本,经自己实验可行。直接上代码: #-...

tensorflow 使用flags定义命令行参数的方法

tf定义了tf.app.flags,用于支持接受命令行传递参数,相当于接受argv。 import tensorflow as tf #第一个是参数名称,第二个参数是默认值,第三个...

python中日志logging模块的性能及多进程详解

python中日志logging模块的性能及多进程详解

前言 Java 中最通用的日志模块莫过于 Log4j 了,在 python 中,也自带了 logging 模块,该模块的用法其实和 Log4j 类似。日志是记录操作的一种好方式。但是日...