如何比较2个不同数据框中的数据

时间:2019-01-14 14:50:38

标签: python pandas dataframe

我正在尝试在Python中比较2个熊猫数据框的数据。我得到了在他们两个中都很常见的一列,但是他们有不同的名字。在第一个中,列的名称为“文件”,在第二个中,列的名称为“Códigodatransação”。无论如何,我创建了此函数以比较数据,但是在这些行中却出现了错误...为什么会发生这种情况?

var httpContext = Request.RequestContext.HttpContext;
if (httpContext == null) return;

var routeData = routes.GetRouteData(httpContext);

var slug = routeData.Values["slug"] as string;

var urlRecord = urlRecordService.GetBySlugCached(slug);

//process URL
switch (urlRecord.EntityName.ToLowerInvariant()) {
  case "product":
    {
      data.Values["controller"] = "Product";
      data.Values["action"] = "ProductDetails";
      data.Values["productid"] = urlRecord.EntityId;
      data.Values["SeName"] = urlRecord.Slug;
    }
    break;
  case "category":
    {
      data.Values["controller"] = "Catalog";
      data.Values["action"] = "Category";
      data.Values["categoryid"] = urlRecord.EntityId;
      data.Values["SeName"] = urlRecord.Slug;
    }
    break;
    .....
}

1 个答案:

答案 0 :(得分:0)

使用pd.DataFrame.where,您可以获取一个DataFrame,其中包含两个DataFrame中位于相同位置的值

df1.where(df1.values==df2.values)
  

编辑:在发表评论后,此方法应该有效:

A = pd.DataFrame([4,2,3], columns = ['Number'])
B = pd.DataFrame([2,5,6], columns = ['Number'])

a = set(A['Number'])
b = set(B['Number'])

my_set = set(a | b) #put every value in a set, so that you don't check each column twice

for i in my_set:
    if i in A['Number'].values:
        if i in B['Number'].values:
            print(str(i) + ' is in both DataFrames')
        else :
            print(str(i) + ' is in A but not in B')
    else: #if the value is not in A, it is obviously in B
        print(str(i) + ' is in B but not in A')

输出:

2 is in both DataFrames 
3 is in A but not in B 
4 is in A but not in B
5 is in B but not in A 
6 is in B but not in A