首页 > 编程 > Python > 正文

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

2019-11-25 13:21:45
字体:
来源:转载
供稿:网友

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数据之间求补集的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表