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

yipeiwu_com5年前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遍历数组的方法小结

本文实例总结了python遍历数组的方法。分享给大家供大家参考。具体分析如下: 下面介绍两种遍历数组的方法,一种是直接通过for in 遍历数组,另外一种是通过rang函数先获得数组长度...

tensorflow: 查看 tensor详细数值方法

问题 tensor详细数值 不能直接print打印: import tensorflow as tf x = tf.constant(1) print x 输出: Tensor...

Python3模拟登录操作实例分析

Python3模拟登录操作实例分析

本文实例讲述了Python3模拟登录操作。分享给大家供大家参考,具体如下: 模拟登录_要求: 1. 用户输入账号密码进行登录 2. 用户信息保存在文件内 3. 用户密码输入错误三次后锁定...

在Linux下使用Python的matplotlib绘制数据图的教程

在Linux下使用Python的matplotlib绘制数据图的教程

如果你想要在Linxu中获得一个高效、自动化、高质量的科学画图的解决方案,应该考虑尝试下matplotlib库。Matplotlib是基于python的开源科学测绘包,基于python软...

使用Python对IP进行转换的一些操作技巧小结

Python Socket模块中包含一些有用IP转换函数,说明如下: socket.ntohl(x) // 类似于C语言的ntohl(x) 把32位正整数从网络序转换成...