求在df1不在df2的数据
使用Pandas中的drop_duplicates()和merge()方法来计算两个相同列的DataFrame的差异。
例子:
import pandas as pd
# 创建两个相同列的DataFrame
df1 = pd.DataFrame({'value': ['A', 'B', 'C', 'D', 'E']})
df2 = pd.DataFrame({'value': ['C', 'D', 'E', 'F', 'G']})
print("df1:")
print(df1)
print("df2:")
print(df2)
# 使用merge()方法获取df1中不在df2中的元素
diff = pd.merge(df1, df2, how='outer', indicator=True)
result = diff[diff['_merge'] == 'left_only'].drop(columns=['_merge'])
# 打印结果
print("result:")
print(result)
标签:df,求差,df1,DataFrame,df2,merge,pd,print,pandas
From: https://www.cnblogs.com/afra17/p/17478001.html