对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 bluetooth蓝牙信息获取蓝牙设备类型的方法

python 获取蓝牙设备类型 扫描蓝牙设备获取到的信息中,无法判断扫描到的蓝牙设备属于什么类型的设备。 扫描蓝牙信息使用的是python 里面的bluetooth模块。 首先扫描出来的...

Python 一句话生成字母表的方法

List >>> [chr(i) for i in range(97,123)] ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',...

python将字符串以utf-8格式保存在txt文件中的方法

python将字符串以utf-8格式保存在txt文件中的方法

如下所示: #ltp_data 字符串 写进777.txt 1、 def save(filename, contents): fh = open(filename, 'w',...

python提取图像的名字*.jpg到txt文本的方法

如下所示: <span style="font-size:18px;"># -*- coding:utf-8 -*- import sys sys.path.append...

浅谈python日志的配置文件路径问题

如下所示: import logging import logging.config logging.config.fileConfig(path) logger = logging...