1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| df1 = pd.DataFrame([['tom', 10, '男'], ['alice', 11, '女']], columns=['name', 'age', 'gender'])
df2 = pd.DataFrame([['tom', 10, '男'], ['ben', 12, '男']], columns=['name', 'age', 'gender'])
print(df1)
print(df2)
# 交集
df_intersect = pd.merge(df1, df2, on=['name', 'age', 'gender'], how='inner')
print("intersect:", "-" * 35)
print(df_intersect)
# 并集
print("union:", "-" * 35)
df_union = pd.concat([df1, df2])
df_union.drop_duplicates(inplace=True)
print(df_union)
# 差集
print("subtract:", "-" * 35)
df_subtract = pd.concat([df1, df2])
df_subtract.drop_duplicates(keep=False, inplace=True)
print(df_subtract)
|