Pandas并集差集交集与isin

并集:

# 实现1
df_union = pandas.concat([df1, df2])
# 实现2
df_union = pandas.merge(df1, df2, on=df1.columns.to_list(), how="outer")

差集:

# 实现1
df_diff = df_union.append(df1).drop_duplicates(subset=df_union.columns.to_list(), keep=False)
# 实现2
df_diff = df_union[~df_union.isin(df1)].dropna(how="all")

交集:

# 实现1
df_in = pandas.merge(df_union, df1, on=df1.columns.to_list())
#实现2
df_in = df_union[df_union.isin(df1)].dropna(how="all")

上一篇:csv文件中单引号转双引号,快捷方法,pandas apply方法


下一篇:python——pandas进阶知识