1.场景,对于colums都相同的dataframe做过滤的时候
例如:
1
2
3
4
5
6
7
8
9
10
|
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中存在的行):
1
2
3
4
|
df1 = df1.append(df2) df1 = df1.append(df2) df1 = df1.drop_duplicates(subset = [ 'name' , 'age' , 'sex' ],keep = False ) print (df1) |
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# -*- 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:\n%s\n\n" % df1) df2 = DataFrame([[ 'a' , 10 , '男' ], [ 'b' , 11 , '女' ]], columns = [ 'name' , 'age' , 'sex' ]) print ( "df2:\n%s\n\n" % df2) # 取交集 print ( "交集:\n%s\n\n" % pd.merge(df1,df2,on = [ 'name' , 'age' , 'sex' ])) # 取并集 print ( "并集:\n%s\n\n" % 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中存在的行):\n%s\n\n" % df1.drop_duplicates(subset = [ 'name' , 'age' , 'sex' ],keep = False )) |
截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/niuniuc/p/9873134.html