pandas DataFrame 交集并集补集

Posted niuniuc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pandas DataFrame 交集并集补集相关的知识,希望对你有一定的参考价值。

1.场景,对于colums都相同的dataframe做过滤的时候

例如:

df1 = DataFrame([[‘a‘, 10, ‘男‘], 
[‘b‘, 11, ‘男‘],
[‘c‘, 11, ‘女‘],
[‘a‘, 10, ‘女‘],
[‘c‘, 11, ‘男‘]],
columns=[‘name‘, ‘age‘, ‘sex‘])

df2 = DataFrame([[‘a‘, 10, ‘男‘],
[‘b‘, 11, ‘女‘]],
columns=[‘name‘, ‘age‘, ‘sex‘])

取交集:print(pd.merge(df1,df2,on=[‘name‘, ‘age‘, ‘sex‘]))
取并集:print(pd.merge(df1,df2,on=[‘name‘, ‘age‘, ‘sex‘], how=‘outer‘))
取差集(从df1中过滤df1在df2中存在的行):
df1 = df1.append(df2)
df1 = df1.append(df2)
df1 = df1.drop_duplicates(subset=[‘name‘, ‘age‘, ‘sex‘],keep=False)
print(df1)

代码:
# -*- coding:utf-8 -*-
__version__ = ‘1.0.0.0‘
"""
@brief : 简介
@details: 详细信息
@author : zhphuang
@date : 2018-10-29
"""

import pandas as pd
from pandas import *

df1 = DataFrame([[‘a‘, 10, ‘男‘],
[‘b‘, 11, ‘男‘],
[‘c‘, 11, ‘女‘],
[‘a‘, 10, ‘女‘],
[‘c‘, 11, ‘男‘]],
columns=[‘name‘, ‘age‘, ‘sex‘])
print("df1: %s " % df1)
df2 = DataFrame([[‘a‘, 10, ‘男‘],
[‘b‘, 11, ‘女‘]],
columns=[‘name‘, ‘age‘, ‘sex‘])
print("df2: %s " % df2)
# 取交集
print("交集: %s " % pd.merge(df1,df2,on=[‘name‘, ‘age‘, ‘sex‘]))

# 取并集
print("并集: %s " % pd.merge(df1,df2,on=[‘name‘, ‘age‘, ‘sex‘], how=‘outer‘))

# 从df1中过滤df1在df2中存在的行,也就是取补集
df1 = df1.append(df2)
df1 = df1.append(df2)
print("补集(从df1中过滤df1在df2中存在的行): %s " % df1.drop_duplicates(subset=[‘name‘, ‘age‘, ‘sex‘],keep=False))
截图 
技术分享图片

 

 














































以上是关于pandas DataFrame 交集并集补集的主要内容,如果未能解决你的问题,请参考以下文章

你真的了解数组吗?(交集差集并集补集在项目中的使用)

java集合求差值和并集!

EXCEL交集并集问题

MySQL取并集去掉交集怎么写

JS - 计算两个数组的交集差集并集补集(多种实现方式)

Python求两个list的交集并集补集对称差集的两种方法